Difference Between Class Level And Object Level Members
Class Level and Object Level Members in python
1) Static Members(Class Level Members)
2) Instance Members(Object Level Members)
Let’s learn about the differences in these categories-
1) Static members(Class level members)-
- As per procedural/function programming language-
Ex-
x=100 def display(): print(x) display() Output- 100
- As Per Object Oriented Programming Language-
Ex-
class Test: x = 100 def display(): print(Test.x) Test.display() Output- 100
Class-level members are attributes and methods that belong to the class itself rather than to instances of the class. They are shared among all instances of the class and are typically used for data or functionality that is common to the entire class.
Class-level attributes are defined inside the class but outside any instance methods, typically at the top of the class definition. They can be accessed using the class name or through an instance of the class.
Class-level methods are methods that are defined using the @classmethod
decorator and have access to the class itself as their first parameter (usually named cls
). These methods can be called on the class itself and can be used to modify class-level attributes or perform operations that are related to the class as a whole.
class MyClass:
class_variable = 0 # Class-level attribute
def __init__(self, instance_variable):
self.instance_variable = instance_variable
@classmethod
def increment_class_variable(cls):
cls.class_variable += 1
obj1 = MyClass(1)
obj2 = MyClass(2)
MyClass.class_variable = 10 # Accessing class-level attribute
obj1.increment_class_variable() # Accessing class-level method
print(MyClass.class_variable) # Output: 11
2) Object Level Members(Instance)-
- – ‘Self’ variable need to be used as the first argument of that function.
- – ‘Self’ is not the keyword but it is always recommended to use this name to define instance method.
- – Instead of ‘Self’ we can also use anything else like ‘Self1’ or etc but not recommended to do that.
Ex-
class MainClass: def display(Self): print("Inside display") # MainClass.display()---> Error as this can only be called with the help of object. obj=MainClass() obj.display() Output- Inside display 2) -> class TestClass: def display(Self): print("Inside display") def main(self): TestClass.display(self) ob = TestClass() TestClass.display(ob) obj = TestClass() obj.main() Output- Inside display Inside display
Another Ex-
class Test: x = 100 def display(): print("Inside Display") def main(): Test.display() print(Test.x) Test.main() Output- Inside Display 100
Object-level members are attributes and methods that belong to instances of the class. Each instance has its own set of object-level attributes, and object-level methods are used to perform operations specific to an instance.
Object-level attributes are typically defined within the constructor (__init__
) method and are accessed using the instance name (self.attribute_name
).
Object-level methods are regular methods defined inside the class and can access and manipulate object-level attributes. They are called on specific instances of the class.
class MyClass:
def __init__(self, instance_variable):
self.instance_variable = instance_variable
def increment_instance_variable(self):
self.instance_variable += 1
obj1 = MyClass(1)
obj2 = MyClass(2)
obj1.increment_instance_variable() # Accessing object-level method
print(obj1.instance_variable) # Output: 2
print(obj2.instance_variable) # Output: 2 (obj2 is not affected)