python set class variable 1

python set class variable

class MyClass:
    static_elem = 123 # This is a static element belonging to the class

    def __init__(self):
        self.object_elem = 456 # This is an instance / object element

Here is what the above code is Doing:
1. We create a class called MyClass
2. We create a static element called static_elem and assign it the value 123
3. We create a method called __init__ which is a special method that is called when an object is created
4. We create an instance element called object_elem and assign it the value 456

Now let’s create an object of the class MyClass and see what happens:

Similar Posts