Last active
October 5, 2020 04:07
-
-
Save farisachugthai/f65552cc6d2cb7a69d08380af995145d to your computer and use it in GitHub Desktop.
Initializing classes in python.
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""First example. Goes over an example where properties are really needed.""" | |
class InitializationExample: | |
"""Be wary when you assign data to an attribute in a class's ``__init__``. | |
If the value could change, it's probably better to avoid it. | |
If other attributes depend on the value of the first attribute, avoid | |
it like the plague!! | |
Let's look at the example below to elaborate on what I mean. | |
At some point I may show an example of why properties are useful if you need to | |
do so. | |
""" | |
def __init__(self): | |
# dont assign data to self. assign classes or functions | |
self.hereswhy = 5 | |
self.uhoh = self.hereswhy + 3 | |
def fuckup(self): | |
# Lets change this value. | |
self.hereswhy = 7 | |
# uhoh doesnt update when hereswhy does :/ | |
print(self.uhoh) | |
def run(): | |
"""Run the example and add a doctest for good measure. | |
>>> i = InitializationExample() | |
>>> print(i.hereswhy) | |
5 | |
>>> print(i.uhoh) | |
8 | |
>>> i.fuckup() | |
8 | |
Yeah so we were definitely hoping for 10 from the last one. | |
""" | |
i = InitializationExample() | |
print(i.hereswhy) | |
print(i.uhoh) | |
i.fuckup() | |
run() |
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""A silly example of how to initialize classes in different ways. | |
Used as an example of how self works to a friend. | |
""" | |
def jackoff(): | |
raise NotImplementedError | |
class InputOptions: | |
def __init__(self, selection=None): | |
"""Init gets called as soon as you instantiate the class. | |
So this block of code gets run every time you the following.:: | |
>>> opt = InputOptions(selection=2) | |
So don't make this absurdly slow. | |
""" | |
if selection is None: # come up with some reasonable default | |
selection = [1] | |
self.selection = [selection] | |
def selections(self, selection_override=3): | |
"""So now we have self.selection and the override to work with. | |
Hopefully that defined enough state for us to make some decisions. | |
""" | |
print(f'Selection was: {self.selection}') | |
print(f'Selection_override was: {selection_override}') | |
if selection_override == 1: | |
jackoff() | |
elif self.selection == [4]: # will execute | |
print("Why hello you sexy bastard.") | |
# example of how to handle a list | |
elif selection_override == [1, 2, 3]: | |
print( | |
"Let's use print statements to verify that this code is executing as expected." | |
) | |
def main(): | |
opt = InputOptions(4) | |
opt.selections(2) | |
print(vars(opt)) | |
second = InputOptions([2, 3, 4]) | |
second.selections([1, 2, 3]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment