Advance Your Python Skills

Attributes in Python — 6 Concepts to Know

Better understand Python as a unique OOP language.

Yong Cui, Ph.D.
Jun 23 · 8 min read
Image for post
Image for post
Photo by Federico Tasin on Unsplash

1. Class Attributes

To better manage data in our projects, we often need to create custom classes. In Python, classes are objects too, which means that they can have their own attributes. Let’s see an example.

Class Attributes
Class Attributes Accessed by Instances

2. Instance Attributes

With custom classes, we can also set attributes to instance objects. These attributes are known as instance attributes, which means that they are instance-specific data. Let’s continue with the Dog class.

Instance Attributes
Instance Attributes — Example
Attribute Override

3. Functions As Attributes

In Python, everything is an object, and previously, I already mentioned that classes are objects. Moreover, functions are Python objects. Within a class, we can define functions, which are commonly referred to as methods. Depending on how the functions are used, we can further categorize them as class methods, static methods, and instance methods. It’s not essential here to understand these differences (see my previous articles: this and that, if you want to learn them).

Updated Class With Methods
Updated Class — dir
Call Instance Method

4. Private Attributes

If you have any experience in OOP, you shouldn’t be unfamiliar with the existence of access modifiers, such as public, private, and protected. These modifiers restrict the scope of where the modified attributes and functions can be accessed. However, you rarely hear such a discussion in Python. Practically, all Python attributes are public, if we borrow the terminology in the OOP world. As shown above, the class and instance attributes are all freely accessible where these classes and instances are accessible. Thus, strictly speaking, there are no real private or protected attributes (to be discussed next) in Python. We’re just using these terms analogously, such that it’ll be easier for programmers coming from other OOP background to appreciate related coding conventions (yes, just a convention, not reinforced as real access control).

Private Attributes
Name Mangling

5. Protected Attributes

In the last section, we talked about private attributes, but how about protected attributes? The counterparts of protected attributes in Python are attributes whose names have only one underscore. Unlike the double underscores which will result in mangling, the single underscore prefix doesn’t change how Python interpreter handles these attributes — It’s merely a convention in the Python programming world to denote that they (i.e., the coder) doesn’t want you to access these attributes. However, again, if you insist on accessing them, you can still do that. Let’s see the code below.

Protected Attributes
Image for post
Image for post
No Auto-completion Suggestions for Non-Public Attributes

6. Properties

Some OOP languages use attributes and properties interchangeably, but they are different concepts in Python. As discussed, attributes in Python are a broad concept that can include different kinds of attributes and functions. However, properties in Python are essentially functions that are decorated with the @property decorator. The decorated function will provide the callers with easier access to these properties. They can often be used together with the protected attributes. Let’s see an example.

Property Definition
Use Property
Property Setter
Set Property

Conclusions

In this article, we reviewed 6 important concepts related to attributes in Python, particularly in the context of custom classes. Understanding these concepts can help you better master Python as a unique OOP language.

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Yong Cui, Ph.D.

Written by

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Yong Cui, Ph.D.

Written by

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store