Last active
August 29, 2015 14:13
-
-
Save illuzian/0ce38540cb501de86804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass(object): | |
def __init__(self, param1=False, param2=False): | |
self.param1 = param1 | |
self.param2 = param2 #set some variables you could pass in at instantiation eg a = SomeClass(param1=5, param2=6) | |
# Make some empty variables you plan to use later: | |
self.some_array = [] | |
self.some_dict = {} | |
self.some_var = False | |
# First method | |
def some_method(self, method_param=False): | |
self.some_var = method_param * 2 | |
# No return value since this one was just made to set the internal variable, this is an example you could do the return here | |
def get_some_var(self): | |
# Returns the instances of the classes value of var self.some_var | |
return self.some_var | |
# A static method | |
@staticmethod | |
def static(var=False): | |
# Notice how no self is passed in? No access to self.anything will work | |
return var / 2 | |
# A property method | |
@property | |
def times_x(self, x=1): | |
return self.some_var * x | |
################ EXECUTING THE CLASS ##################### | |
instance1 = SomeClass(param1=4, param2=10) | |
# You can now access class methods | |
instance1.some_method(method_param=10) # Inside this instances, self.some_var = 10 | |
# Everything defined as self.something is actually a property | |
print instance1.some_var | |
#> 10 | |
print instance1.get_some_var | |
#> 10 | |
# get_some_var does the same thing as accessing the defined property, it's useful if you need to do something first | |
print instance1.param1 | |
#> 4 | |
# If we created a new instance eg | |
instance2 = SomeClass(param1=10, param2=20) | |
# It now has it's own values | |
print instance2.param1 | |
#> 20 | |
print instance1.param1 | |
#> 10 | |
# In most cases with classes you'd want to instantiate it but as you've seen some imports let you do something.something aka a staticmethod | |
print SomeClass.static(10) | |
#> 5 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vehicle(object): | |
def __init__(self, size=False): | |
self.size = size | |
class Car(Vehicle): | |
def __init__(self, wheel_size=False): | |
# Init has now been overriden so nothing is available from the Vehicle's init, you can fix that with: | |
super(Car, self).__init__ | |
# You always pass in the current class (it knows what the parent is since you passed it in with class Car(Vehicle) | |
# You then put in the method you want to super in this case .__init__ (__init__ being a method) | |
self.wheel_size = wheel_size | |
# We now have access to self.size and self.wheel_size | |
# If you wanted to make a care all you do is | |
a = Car(wheel_size=25, size=100) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You might want to do something like this | |
class Lotto(object): | |
def __init__(self): | |
self.some_shit = False | |
def calculate_some_shit(self): | |
self.some_shit = [i*10 for i in range(10)] | |
return self.some_shit | |
class SomeUiShit(object): # You might be subclassing here so object would be replaced with whatever tkinter requires | |
def __init__(self): | |
pass | |
def show_ui(self): | |
pass | |
def put_numbers_on_ui(self, numbers): | |
pass # Do some shit | |
lotto = Lotto() | |
ui = SomeUiShit() | |
ui.show_ui() | |
ui.put_numbers_on_ui(lotto.calculate_some_shit()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment