Class Functions and Class Variables

Class Functions and Class Variables

Tutorial for writing and using class functions and variables

·

3 min read

Class Variables

Class variables are variables that stay within the class. It's basically a variable you'll need in using a function.

Calling

class Vector:

    vector_count = 0
    value = 1.5

    def __init__(self, *args):
        '''
        Create a vector, example : v = Vector(5, 10)
        '''
        if len(args) <= 1:
            self._x, self._y = 0, 0
        else:
            self._x, self._y = args
        vector_count += 1

    def __repr__(self):
        return 'Vector(%r, %r)' % (self._x, self._y)


    def get_calc(self):
        return Vector(self._x * value, self._y * value)

v1 = Vector(8, 2)
v2 = Vector(10, 2)
v3 = Vector(2, 3)
v4 = Vector()

print(v1.get_calc)
print(Vector.vector_count)

The code above will print what?

A: the init function was called 4 times, each when the v1, v2, v3, v4 was created. So the vector_count will be 4. the .get_calc function returns the Vector multiplied by the value, which is 1.5. print(v1.get_calc) will print Vector(12, 3) (81.5, 21.5). print(Vector.vector_count) will print 4.

Class Functions

Why? In order to change class variables, you have to directly quote them: for instance, Vector.value = 1. However, in bigger projects with more variables in one class intertwined, it's going to be hard to

Creating class functions is similar to creating instance functions, but you have to write @classmethod before you define a class function. Also, you need to write cls instead of self.

Calling class functions are quite different from that of instance functions. You have to directly quote the class name: in this case Vector. So, instead of writing self inside the functions, which refer to the instance, you write cls, for class.

class Vector:

    vector_count = 0
    value = 1.5

    def __init__(self, *args):
        '''
        Create a vector, example : v = Vector(5, 10)
        '''
        if len(args) <= 1:
            self._x, self._y = 0, 0
        else:
            self._x, self._y = args
        vector_count += 1

    def __repr__(self):
        return 'Vector(%r, %r)' % (self._x, self._y)

    def get_calc(self):
        return Vector(self._x * value, self._y * value)

    @classmethod
    def change_value(cls, n)
        if n ==value:
            print('please input a different number')
            return
        value = n
        print('succesfully changed the value!')

v1 = Vector(8,2)

print(v1.get_calc)

Vector.change_value(2)

print(v1.get_calc)

Now, for the first print statement, it will print Vector(12, 3) (because the value is still 1.5)

However, for the second print statement, before the statement, we called the class function and changed the value to 2. So for the second call of get_calc, it multiples the vector by 2, printing Vector(16, 4).

appendix

For the examples, I just used vectors since it's the easiest way to explain class functions. This particular example might confuse you since it seems like class functions can be replaced with instance functions without any errors. However, in big projects, again, will be countless class variables and complex, intertwined functions. You WILL NEED CLASS FUNCTIONS for big projects. Even if you don't now, please take a moment to understand the concept and put it into your head.

Happy Coding!