Python Classes #
Simple Example #
class Something:
pass
something_1 = Something()
something_2 = Something()
Instance #
Variables per instance:
something_1.name = "Arnold"
something_1.age = 10
something_2.name = "Bob"
something_2.age = 20
init method #
Cleaner way to assign variables with classes:
class Something:
def __init__(self, name, age):
self.name = name
self.age = age
something_1 = Something('Arnold', 10)
something_2 = Something('Bob', 20)
Instantiation automatically runs __init__
.
Methods #
Add methods thusly:
class Something:
def __init__(self, ...):
...
def another_method(self):
...
The another_method
might invoke a variable or method from within the class:
def another_method(self):
... self.name ... # assuming self.name gets defined higher up in the __init__ chunk
something_1.another_method()
Regular Methods #
By default, takes instance as first argument (self
).
Class Methods #
Class methods act on the class itself.
A class is the first argument in a class method. Convention is to refer to the class variable with name cls
.
Set up class method with a decorator (@classmethod
).
class Something:
def __init__(self, ...):
...
@classmethod
def some_class_method(cls, <argument>):
cls.some_variable = some_value
Once defined, use as follows:
Something.some_class_method(<argument>)
This some_class_method
works on the entire class, percolates down to instances derived from class.
Class methods can be used as alternative constructors
as different ways to create objects.
# alternative constructors
class Something:
def __init__(self, something1, something2):
...
@classmethod
def some_constructor(cls, <argument>):
out1, out2 = <argument>.<do something> # assuming doing something to the argument creates multiple outputs
return cls(out1, out2) # basically takes outputs and rolls them into the class
Static Methods #
Method should be a static method if the method doesn’t interact with instance or class anywhere.
General utility method, works on its own in isolation, doesn’t pass any thing (neither instance or class).
No self
(static) or cls
(class) argument used.
Use @staticmethod
decorator to create static method.
class Something:
def __init__(self, ...):
...
@staticmethod
def some_static(<argument>):
... <argument> ...
Magic Methods #
Inheritance #
Allows ability for a child class to inherit methods or variables from parent class.
Simple example:
class Something:
...
class SomethingSub(Something):
...
Tada. That’s it.
The child can now be modified to be different from the parent without affecting the parent.
Possible to explicitly inherit particular elements from the parent, and then introduce other values with the subclass using the super().__init__(<values from parent>)
.
class SomethingSub(Something):
def __init__(self, value_from_parent1, value_from_parent2, new_value):
super().__init(value_from_parent1, value_from_parent2)
# Something.__init__(self, value_from_parent1, value_from_parent2) # a less clean option
self.new_value = new_value
# now instantiate
instance_1 = SomethingSub(<valeue1>, <value2>, <new_value_input>)
Method resolution order
- the chain of inheritance through classes.
Can find details about inheritance (variables, methods, etc.) with help
:
print(help(SomethingSub))
Check if something is an instance of some class or a sub-subclass:
# True
isinstance(instance_1, Something)
isinstance(instance_1, SomethingSub)
Also check if subclass:
# True
issubclass(instance_1, SomethingSub)
Class Variables #
A variable… for a class.
class Something:
some_variable = <some sort of value>
def __init__(self, ...):
...
def another_method(self):
... self.some_variable ...
Namespace #
Check out the namespace:
# for the class
print(Something.__dict__)
# for an instance
print(something_1.__dict__)