pytutorial/python_basics/class/README.md
David Rotermund bb748a6e7e
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-05 16:48:54 +01:00

15 KiB

Class

{:.no_toc}

* TOC {:toc}

The goal

Class has a very important job as a core container type in Python. It is really hard to find a good overview how to use them in a good practice manner.

Questions to David Rotermund

Basics

Class is the core component of object-oriented programming (OOP). A class is an object that can contain data i.e. class variables (also called fields, attributes or properties) and code, called class methods. Here we will look at the basic Python class. For data science there is a simplified derivation available called dataclass.

class MostSimplestClass:
    pass

Instance

What is an instance? After defining a class we need to put it into memory. Every instance of a class generated in memory is... well... an instance. Here is an example how we create two instances of a class: 

class SimpleClass(object):
    variable_a: int

instance_a = SimpleClass()
instance_a.variable_a = 1

instance_b = SimpleClass()
instance_b.variable_a = 2

print(instance_a.variable_a)  # -> 1
print(instance_b.variable_a)  # -> 2

In this example we create an instance with SimpleClass() . Reading the content of the ( ) it depends on how you define the method __init__ . By default it doesn't take any arguments from you.

Variables

Code and data are the components a class is made of. Let us look at the data (i.e. variables) first. 

Note: If we want to use a variable (or a method) of the class from within the class we need to use the prefix self.

BEST (in my opinion)

To make a long story short, you want to do it like this:

class MostSimplestClass:
    a: int
    b: list

    def __init__(self):
        self.a = 0
        self.b = []

or use direclty a dataclass when possible!

BAD: How we DON'T want to do it

We have the option to add variables from outside to the class:

class MostSimplestClass:
    pass

instance = MostSimplestClass()
instance.a: int = 1
print(instance.a)

or you can define variables in any of the methods:

class MostSimplestClass:
    def __init__(self):
        self.a: int = 1

    def first_run(self):
        self.b: int = 2


instance = MostSimplestClass()
print(instance.a) # -> 1
instance.first_run()
print(instance.b) # -> 2

This is valid and working code. However, you will create a mess. If someone else (or yourself after a few weeks later) needs to look at your source code, there will be tears and hate.  

Better, but not good, is to define everything in __init__():

class MostSimplestClass:
    def __init__(self):
        self.a: int = 1
        self.b: int = 0

    def first_run(self):
        self.b = 2


instance = MostSimplestClass()
print(instance.a)  # -> 1
instance.first_run()
print(instance.b)  # -> 2

However, if your __init__ is more complex, you have to search through it for finding the variables. 

GOOD

For a better code quality we should look at the Python dataclass. 

class MostSimplestClass:
    a: int = 1
    b: int = 0

    def first_run(self):
        self.b = 2


instance = MostSimplestClass()
print(instance.a)  # -> 1
instance.first_run()
print(instance.b)  # -> 2

Now every class variable is defined in the beginning at one place. Easy to find. Easy to look through. We also add the type of the variable here, since we should provide it at the first use of the variable. All question or uncertainty where the first appearance of a variable will be is removed. 

However, we need to talk about mutable objects and the problem with mutable objects and their initialization.

The danger of initializing mutable variables

  • immutable (e.g. numbers, string, tuples) : "An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored."
  • mutable (list, dictionary, set) : "Mutable objects can change their value but keep their id()."

This is how the problem looks like:

class SimpleClass:
    a: int = 0
    b: list = []

instance_a = SimpleClass()
instance_b = SimpleClass()

instance_a.a = 1
print(instance_a.a)  # -> 1
print(instance_b.a)  # -> 0

instance_a.b.append("X")
print(instance_a.b)  # -> ['X']
print(instance_b.b)  # -> ['X']

The correct way to handle it is:

class SimpleClass:
    a: int
    b: list

    def __init__(self):
        self.a = 0
        self.b = []


instance_a = SimpleClass()
instance_b = SimpleClass()

instance_a.a = 1
print(instance_a.a)  # -> 1
print(instance_b.a)  # -> 0

instance_a.b.append("X")
print(instance_a.b)  # -> ['X']
print(instance_b.b)  # -> []

Since you have defined the variables in the beginning of the class and know the type of it (due to the type annotation), you can just copy the list into the constructor __init__ and initialize the variables there. 

The alternative: dataclass

Maybe you want to consider using dataclass where it is possible instead. There you are protected against this error:

from dataclasses import dataclass

@dataclass
class SimpleClass:
    a: list = [] 
ValueError: mutable default <class 'list'> for field a is not allowed: use default_factory

And you get there a nice way to initialize mutable objects safely via default_factory: 

from dataclasses import dataclass, field

@dataclass
class SimpleClass:
    a: list = field(default_factory=list)

instance_a = SimpleClass()
instance_b = SimpleClass()

instance_a.a.append("X")
print(instance_a.a)  # -> ['X']
print(instance_b.a)  # -> []

@property()

If a variable starts with one _ this tells us that it is "private" and we shouldn't touch it directly with our dirty hands from the outside. However, we can use @property to control the communication with the outside world:  

class SimpleClass:
    _a: int = 0

    @property
    def a(self) -> int:
        return self._a

instance = SimpleClass()

print(instance.a)  

instance.a = 1  
0
[...]
AttributeError: property 'a' of 'SimpleClass' object has no setter

If we want to then we can allow writing and deleting of the variable too:

class SimpleClass:
    _variablename: int = 0

    @property
    def variablename(self) -> int:
        return self._variablename

    @variablename.setter
    def variablename(self, value):
        self._variablename = value

    @variablename.deleter
    def variablename(self):
        del self._variablename


instance = SimpleClass()

print(instance.variablename)  # -> 0

instance.variablename = 1
print(instance.variablename)  # -> 1

__slots__

They say:

  • __slots__ allow us to explicitly declare data members (like properties) and deny the creation of __dict__ and __weakref__ (unless explicitly declared in __slots__ or available in a parent.) The space saved over using __dict__ can be significant. Attribute lookup speed can be significantly improved as well. Let's test if they are really smaller (Note: I will use the memory_profiler module which doesn't work with ipython: pip install memory_profiler ): 

With slots:

from memory_profiler import profile

class SimpleClass:
    __slots__ = ["variable_a", "variable_b", "variable_c"]
    variable_a: int
    variable_b: float
    variable_c: float

    def __init__(self, value) -> None:
        self.variable_a = value
        self.variable_b = value * 2
        self.variable_c = value * 3


@profile
def main():
    instances = []
    for i in range(0, 100000):
        instances.append(SimpleClass(i))

main()

Note: The default values are set in the __init__ and in the section not above. __slots__ doesn't like it if you provide directly default values. 

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    17     39.2 MiB     39.2 MiB           1   @profile
    18                                         def main():
    19     39.2 MiB      0.0 MiB           1       instances = []
    20     56.0 MiB     13.9 MiB      100001       for i in range(0, 100000):
    21     56.0 MiB      2.8 MiB      100000           instances.append(SimpleClass(i))

Without slots:

from memory_profiler import profile


class SimpleClass:
    variable_a: int
    variable_b: float
    variable_c: float

    def __init__(self, value) -> None:
        self.variable_a = value
        self.variable_b = value * 2
        self.variable_c = value * 3


@profile
def main():
    instances = []
    for i in range(0, 100000):
        instances.append(SimpleClass(i))

main()
Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    17     39.4 MiB     39.4 MiB           1   @profile
    18                                         def main():
    19     39.4 MiB      0.0 MiB           1       instances = []
    20     65.2 MiB      6.2 MiB      100001       for i in range(0, 100000):
    21     65.2 MiB     19.6 MiB      100000           instances.append(SimpleClass(i))

Why don't we use it all the time? Well, dynamic maneuvers like this are now denied:  

class SimpleClass:
    __slots__ = ["variable_a", "variable_b", "variable_c"]
    variable_a: int
    variable_b: float
    variable_c: float

    def __init__(self, value) -> None:
        self.variable_a = value
        self.variable_b = value * 2
        self.variable_c = value * 3
        self.b = 1 # AttributeError: 'SimpleClass' object has no attribute 'b'


instances = SimpleClass(1)
instances.a = 1  # AttributeError: 'SimpleClass' object has no attribute 'a'

Methods

Note: If we want to use a method (or a variable) of the class from within the class we need to use the prefix self.

In this example we have defined two methods: __init__ which is the constructor and some_method . Methods are "just" functions defined in a class. 

Typically (except you deal with @classmethod or @staticmethod) the first argument of a method is self.

class SimpleClass:
    variable_a: int

    def __init__(self) -> None:
        self.variable_a = 1

    def some_method(self, input: int) -> int:
        return self.variable_a + input


instance = SimpleClass()
print(instance.some_method(678)) # -> 679

In case we use a function from the outside of the class, we don't see / provide self as an input argument.

class SimpleClass:
    variable_a: int

    def __init__(self) -> None:
        self.variable_a = 1

    def some_method(self, input: int) -> int:
        return self.variable_a + input

    def some_other_method(self, input: int) -> int:
        return self.some_method(input)


instance = SimpleClass()
print(instance.some_other_method(678))  # -> 679

Constructor: init

When we create a new instance, two internal functions of the class are called __new__ and __init__ . __new__ creates it and __init__ customize it. Normally there is no reason to touch __new__.

Note: No return values except None are allowed.

We will otherwise get errors like: "TypeError: __init__() should return None, not 'int'"

The first parameter of __init__ is always self!

class SimpleClass:
    variable_a: int

    def __init__(self) -> None:
        self.variable_a = 1

instance = SimpleClass()

We can add more arguments if we want to. Here an example with one additional argument: 

class SimpleClass:
    variable_a: int

    def __init__(self, value) -> None:
        self.variable_a = value

instance = SimpleClass(1)

__str__ and __repr__

If we print our class then this happens:

class SimpleClass:
    variable_a: int

    def __init__(self, value) -> None:
        self.variable_a = value


instance = SimpleClass(1)
print(instance) # -> <__main__.SimpleClass object at 0x7fcab0600b80>

However, we can add a __str__ function and then we can customize our output:

class SimpleClass:
    variable_a: int

    def __init__(self, value) -> None:
        self.variable_a = value

    def __str__(self) -> str:
        return f"{self.variable_a}"

instance = SimpleClass(1)
print(instance)

But please be aware that there are more than one putative functions for producing output information: 

  • object.__str__(self) human friendly : Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.
  • object.__repr__(self)  unambiguous : Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.