English

Differentiate between class attributes and instance attributes.

Advertisements
Advertisements

Question

Differentiate between class attributes and instance attributes.

Distinguish Between
Advertisements

Solution

Basis of Difference Class Attributes Instance Attributes
Meaning Attributes that belong to the class. Attributes that belong to an individual object.
Declaration Defined directly inside the class body. Usually defined using self inside __init__ or another method.
Sharing Shared by all instances of the class. Separate copy/value for each instance.
Access Accessed using ClassName.attribute or object.attribute. Generally accessed using object.attribute.
Example school = "DPS" self.name = name
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Classes in Python - EXERCISE [Page 62]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 3 Classes in Python
EXERCISE | Q 9. | Page 62
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×