- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we access class attributes using dot operator in Python?
A class attribute is an attribute of the class rather than an attribute of an instance of the class.
In the code below class_var is a class attribute, and i_var is an instance attribute: All instances of the class have access to class_var, which can also be accessed as a property of the class itself −
class MyClass (object): class_var = 2 def __init__(self, i_var): self.i_var = i_var foo = MyClass(3) baz = MyClass(4) print (foo.class_var, foo.i_var) print (baz.class_var, baz.i_var)
This gives the output
(2, 3) (2, 4)
- How do we reference Python class attributes?
- How do we access command line arguments in Python?
- What is a dot operator in Python?
- Built-In Class Attributes in Python
- Class & Instance Attributes in Python
- How do we include attributes for table columns in HTML?
- How can we do Python operator overloading with multiple operands?
- What is dot operator in Java?
- What is dot operator in C++?
- How to define attributes of a class in Python?
- How do we use equivalence (“equality”) operator in Python classes?
- How do we embed custom data attributes on all HTML elements?
- What are built-in class attributes in Python?
- Why do we use comma operator in C#?
- How do we do a file upload using Python CGI Programming?