pwshub.com

Python's property(): Add Managed Attributes to Your Classes

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Managing Attributes With Python's property()

With Python’s property(), you can create managed attributes in your classes. You can use managed attributes when you need to modify an attribute’s internal implementation and don’t want to change the class’s public API. Providing stable APIs will prevent you from breaking your users’ code when they rely on your code.

Properties are arguably the most popular way to create managed attributes quickly and in the purest Pythonic style.

In this tutorial, you’ll learn how to:

  • Create managed attributes or properties in your classes
  • Perform lazy attribute evaluation and provide computed attributes
  • Make your classes Pythonic using properties instead of setter and getter methods
  • Create read-only and read-write properties
  • Create consistent and backward-compatible APIs for your classes

You’ll also write practical examples that use property() for validating input data, computing attribute values dynamically, logging your code, and more. To get the most out of this tutorial, you should know the basics of object-oriented programming, classes, and decorators in Python.

Take the Quiz: Test your knowledge with our interactive “Python's property(): Add Managed Attributes to Your Classes” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python's property(): Add Managed Attributes to Your Classes

In this quiz, you'll test your understanding of Python's property(). With this knowledge, you'll be able to create managed attributes in your classes, perform lazy attribute evaluation, provide computed attributes, and more.

Managing Attributes in Your Classes

When you define a class in an object-oriented programming language, you’ll probably end up with some instance and class attributes. In other words, you’ll end up with variables that are accessible through the instance, class, or even both, depending on the language. Attributes represent and hold the internal state of a given object, which you’ll often need to access and mutate.

Typically, you have at least two ways to access and mutate an attribute. Either you can access and mutate the attribute directly or you can use methods. Methods are functions attached to a given class. They provide the behaviors and actions that an object can perform with its internal data and attributes.

If you expose attributes to the user, then they become part of the class’s public API. This means that your users will access and mutate them directly in their code. The problem comes when you need to change the internal implementation of a given attribute.

Say you’re working on a Circle class and add an attribute called .radius, making it public. You finish coding the class and ship it to your end users. They start using Circle in their code to create a lot of awesome projects and applications. Good job!

Now suppose that you have an important user that comes to you with a new requirement. They don’t want Circle to store the radius any longer. Instead, they want a public .diameter attribute.

At this point, removing .radius to start using .diameter could break the code of some of your other users. You need to manage this situation in a way other than removing .radius.

Programming languages such as Java and C++ encourage you to never expose your attributes to avoid this kind of problem. Instead, you should provide getter and setter methods, also known as accessors and mutators, respectively. These methods offer a way to change the internal implementation of your attributes without changing your public API.

These programming languages need getter and setter methods because they don’t have a suitable way to change an attribute’s internal implementation when a given requirement changes. Changing the internal implementation would require an API modification, which can break your end users’ code.

The Getter and Setter Approach in Python

Technically, there’s nothing that stops you from using getter and setter methods in Python. Here’s a quick example that shows how this approach would look:

In this example, you create a Point class with two non-public attributes ._x and ._y to hold the Cartesian coordinates of the point at hand.

To access and mutate the value of either ._x or ._y, you can use the corresponding getter and setter methods. Go ahead and save the above definition of Point in a Python module and import the class into an interactive session. Then run the following code:

With .get_x() and .get_y(), you can access the current values of ._x and ._y. You can use the setter method to store a new value in the corresponding managed attribute. From the two final examples, you can confirm that Python doesn’t restrict access to non-public attributes. Whether or not you access them directly is up to you.

The Pythonic Approach

Even though the example you just saw uses the Python coding style, it isn’t Pythonic. In the example, the getter and setter methods don’t perform any further processing with the values of ._x and ._y, so you could just have plain attributes instead of methods.

You can rewrite Point in a more concise and Pythonic way:

This code uncovers a fundamental Python principle: exposing attributes to the end user is normal and common. This is cool because you don’t need to clutter your classes with getter and setter methods all the time.

The question is, how do you handle requirement changes that would imply modifying the implementation of attributes without changing your APIs? For example, say that you need to add validation functionality on top of a given attribute. How would you do that if your attribute doesn’t have setter and getter methods where you can put that functionality?

Unlike Java and C++, Python provides handy tools that allow you to change the underlying implementation of your attributes without changing your public API. The most popular approach is to turn your attributes into properties.

Properties represent an intermediate functionality between a plain attribute, or field, and a method. In other words, they allow you to create methods that behave like attributes. With properties, you can change how you compute the target attribute whenever you need to.

For example, you can turn both .x and .y into properties. With this change, you can continue to access them as attributes while having them perform actions when your users access and mutate them.

Python properties allow you to expose attributes as part of your classes’ public APIs. If you ever need to change an attribute’s underlying implementation, then you can conveniently turn it into a property at any time. In the following sections, you’ll learn how to create properties in Python.

Getting Started With Python’s property()

Using Python’s property() is the Pythonic way to avoid getter and setter methods in your classes. This built-in function allows you to turn class attributes into properties or managed attributes. Because property() is a built-in function, you can use it without importing anything. Additionally, property() is implemented in C, which ensures optimized performance.

With property(), you can attach implicit getter and setter methods to given class attributes. You can also specify a way to handle attribute deletion and provide an appropriate docstring for your properties.

Here’s the full signature of property():

The first two arguments take function objects that will play the role of getter (fget) and setter (fset) methods. Python automatically calls these function objects when you access or mutate the attribute at hand.

Here’s a summary of what each argument does:

ArgumentDescription
fgetA function object that returns the value of the managed attribute
fsetA function object that allows you to set the value of the managed attribute
fdelA function object that defines how the managed attribute handles deletion
docA string representing the property’s docstring

The return value of property() is the managed attribute itself. If you access the managed attribute with something like obj.attr, then Python automatically calls fget(). If you assign a new value to the attribute with something like obj.attr = value, then Python calls fset() using the input value as an argument. Finally, if you run a del obj.attr statement, then Python automatically calls fdel().

You can use doc to provide an appropriate docstring for your properties. You and your fellow programmers will be able to read that docstring using Python’s help(). The doc argument is also useful when you’re working with code editors and IDEs that support docstring access.

You can use property() either as a function or decorator to build your properties. In the following two sections, you’ll learn how to use both approaches. However, the decorator approach is more popular in the Python community.

Creating Attributes With property()

You can create a property by calling property() with an appropriate set of arguments and assigning its return value to a class attribute. All the arguments to property() are optional. However, you typically provide at least a getter function.

The following example shows how to create a Circle class with a property that manages its radius:

In this code snippet, you create Circle. The class initializer, .__init__(), takes radius as an argument and stores it in a non-public attribute called ._radius. Then, you define three non-public methods:

  1. ._get_radius() returns the current value of ._radius
  2. ._set_radius() takes value as an argument and assigns it to ._radius
  3. ._del_radius() deletes the instance attribute ._radius

Once you have these three methods in place, you create a class attribute called .radius to store the property object. To initialize the property, you pass the three methods as arguments to property(). You also pass a suitable docstring for your property.

In this example, you use keyword arguments to improve readability and prevent confusion. That way, you know exactly which method goes into each argument.

To give Circle a try, run the following code in your Python REPL:

The .radius property hides the non-public instance attribute ._radius, which is now your managed attribute in this example. You can access and assign .radius directly. Internally, Python automatically calls ._get_radius() and ._set_radius() when needed. When you execute del circle.radius, Python calls ._del_radius(), which deletes the underlying ._radius.

Besides using regular named functions to provide getter methods in your properties, you can also use lambda functions.

Here’s a version of Circle in which the .radius property uses a lambda function as its getter method:

If your getter method’s functionality is limited to returning the current value of the managed attribute, then using a lambda function can be the solution.

Properties are class attributes that manage instance attributes. You can think of a property as a collection of methods bundled together. If you inspect .radius carefully, then you’ll find the raw methods you provided as the fget, fset, and fdel arguments:

You can access the getter, setter, and deleter methods in a given property through the corresponding .fget, .fset, and .fdel attributes.

Properties are also descriptors. If you use dir() to check the internal members of a given property, then you’ll find .__set__() and .__get__() in the list. These methods provide a default implementation of the descriptor protocol.

The default implementation of .__set__(), for example, runs when you don’t provide a custom setter method. This implementation gives you an AttributeError when you try to set the attribute.

Using property() as a Decorator

Decorators are frequently used in Python. They’re typically functions that take another function as an argument and return a new function with added functionality. With a decorator, you can attach pre- and post-processing operations to an existing function.

The decorator syntax consists of placing the name of the decorator function with a leading @ symbol right before the definition of the function you want to decorate:

In this code, @decorator can be a function or class intended to decorate function(). This syntax is equivalent to the following:

The final line of code reassigns the name function to hold the result of calling decorator(function). Note that this is the same syntax you used to create a property in the previous section.

Python’s property() can work as a decorator, so you can use the @property syntax to create your properties quickly:

Circle now is more Pythonic and clean. You don’t need to use method names such as ._get_radius(), ._set_radius(), and ._del_radius() anymore. Now you have three methods with the same descriptive attribute-like name. How’s that possible?

The decorator approach for creating properties requires defining a first method using the public name for the underlying managed attribute, which is .radius in this example. This method should implement the getter logic. In the above example, lines 5 to 9 implement that method.

Lines 11 to 14 define the setter method for .radius. The syntax is different. Instead of using @property again, you use @radius.setter. Why do you need to do that? Take a look at the dir() output:

Besides .fget, .fset, .fdel, and a bunch of other special attributes and methods, property also provides .deleter(), .getter(), and .setter(). These three methods each return a new property.

When you decorate the second .radius() method with @radius.setter on line 11, you create a new property and reassign the class-level name .radius from line 6 to hold it. This new property contains the same set of methods of the initial property on line 6 with the addition of the new setter method provided on line 12. Finally, the decorator syntax reassigns the new property to the .radius class-level name.

The mechanism to define the deleter method is similar. This time, you need to use the @radius.deleter decorator. At the end of the process, you get a full-fledged property with the getter, setter, and deleter methods.

Now, how can you provide suitable docstrings for your properties when you use the decorator approach? If you check Circle again, you’ll notice that you already did so by adding a docstring to the getter method on line 7.

The new Circle implementation works the same as the example in the section above:

First, note that you don’t need to use a pair of parentheses for calling .radius() as a method. Instead, you can access .radius as you would access a regular attribute, which is the primary purpose of properties. They allow you to treat methods as attributes.

Here’s a recap of some important points to remember when you’re creating properties with the decorator approach:

  • The @property decorator must decorate the getter method.
  • The docstring must go in the getter method.
  • The setter and deleter methods must be decorated with the name of the getter method plus .setter and .deleter, respectively.

Up to this point, you’ve learned how to create managed attributes using property() as a function and a decorator. It’s time to think about when you should use properties.

Deciding When to Use Properties

If you check the implementation of your Circle class so far, then you’ll note that its getter and setter methods don’t add extra functionality on top of your attributes.

In general, you should avoid using properties for attributes that don’t require extra functionality or processing. If you do use properties this way, then you’ll make your code:

  • Unnecessarily verbose
  • Confusing to other developers
  • Slower than code based on regular attributes

Unless you need something more than bare attribute access and mutation, don’t use properties. They’ll waste your CPU time and, more importantly, your time.

Finally, you should avoid writing explicit getter and setter methods and then wrapping them in a property. Instead, use the @property decorator. That’s currently the Pythonic way to go.

Providing Read-Only Attributes

Probably the most elementary use case of property() is to provide read-only attributes in your classes. Say you need an immutable Point class that doesn’t allow the user to mutate the original value of its coordinates, x and y. To achieve this goal, you can create Point like in the following example:

Here, you store the input arguments in the attributes ._x and ._y. As you already learned, using the leading underscore (_) in names tells other developers that they’re non-public attributes and shouldn’t be accessed using dot notation, such as in point._x. Finally, you define two getter methods and decorate them with @property.

Now you have two read-only properties, .x and .y, as your coordinates:

Here, .x and .y are read-only properties because you can’t assign new values to them. Their behavior relies on the underlying descriptor that property provides. The default .__set__() implementation on this descriptor raises an AttributeError when you don’t define a setter method.

If you need custom behavior on a read-only property, then you can provide an explicit setter method that raises a custom exception with more elaborate and specific messages:

In this example, you define a custom exception called WriteCoordinateError. This exception allows you to customize the way you implement your immutable Point class. Now, both setter methods raise your custom exception with a more explicit message. Go ahead and give your improved Point a try!

Creating Read-Write Attributes

You can also use property() to provide managed attributes with read-write capabilities. In practice, you just need to provide the appropriate getter (“read”) and setter (“write”) methods to your properties in order to create read-write managed attributes.

For example, say you want your Circle class to have a .diameter attribute. Taking the radius and the diameter in the class initializer seems unnecessary because you can compute the one using the other.

Here’s a Circle that manages .radius and .diameter as read-write attributes but only takes the radius at instance creation time:

Here, you create a Circle class with a read-write .radius property. The getter method just returns the radius value. The setter method converts the radius to a floating-point number and assigns it to the non-public ._radius attribute, which is the variable you use to store the final data.

This new implementation of Circle has a subtle detail that you should note. In this case, the class initializer assigns the input value to the .radius property directly instead of storing it in a dedicated non-public attribute, such as ._radius. Why? Because you must ensure that every radius value—including the initial one—goes through the setter method and gets converted to a floating-point number.

Circle also implements a .diameter attribute as a property. The getter method computes the diameter using the radius. The setter method calculates the radius and stores the result in .radius instead of storing the input diameter in a dedicated attribute. This way of dealing with the diameter makes your class more memory-efficient because you’re only storing the radius.

Here’s how your Circle works:

In this example, both .radius and .diameter work as normal attributes, providing a clean and Pythonic public API for your Circle class.

Providing Write-Only Attributes

You can also create write-only attributes by tweaking the getter method of properties. For example, you can make your getter method raise an exception every time a user accesses the underlying attribute.

Here’s a hypothetical example of handling passwords with a write-only property:

The initializer of User takes the username and password as arguments and stores them in .name and .password, respectively.

You use a property to manage how your class processes the input password. The getter method raises an AttributeError whenever a user tries to retrieve the current password. This turns .password into a write-only attribute:

In this example, you create john as a User instance with an initial password. The setter method hashes the password and stores it in ._hashed_password. Note that when you try to access .password directly, you get an AttributeError. Finally, assigning a new value to .password triggers the setter method and creates a new hashed password.

In the setter method of .password, you use os.urandom() to generate a 32-byte random string as your hashing function’s salt. To generate the hashed password, you use hashlib.pbkdf2_hmac(). Then you store the resulting hashed password in the non-public attribute ._hashed_password. Doing so ensures that you never save the plaintext password in any retrievable attribute.

Putting Python’s property() Into Action

So far, you’ve learned how to use Python’s property() to create managed attributes in your classes. You’ve used property() as a function and as a decorator and learned about the differences between these two approaches. You also learned how to create read-only, read-write, and write-only attributes.

In the following sections, you’ll code a few examples that will help you get a better practical understanding of common use cases of property().

Validating Input Values

Validating input is one of the most common use cases of property() and managed attributes. Data validation is a common requirement in code that takes input from users or other sources that you could consider untrusted. Python’s property() provides a quick and reliable tool for dealing with input validation.

For example, getting back to the Point class, you may require the values of .x and .y to be valid numbers. Since your users are free to enter any type of data, you need to make sure that your points only accept numbers.

Here’s an implementation of Point that manages this requirement:

The setter methods of .x and .y use try … except blocks that validate input data using the Python EAFP (easier to ask forgiveness than permission) style. If the call to float() succeeds, then the input data is valid, and you get Validated! on your screen. If float() raises a ValueError, then the user gets a ValueError with a more specific message.

It’s important to note that assigning the .x and .y properties directly in .__init__() ensures that the validation also occurs during object initialization. Not doing so can lead to issues when using property() for data validation.

Here’s how your Point class works now:

If you assign .x and .y values that float() can turn into floating-point numbers, then the validation is successful, and the value is accepted. Otherwise, you get a ValueError.

This implementation of Point uncovers a fundamental weakness of property(). Did you spot it? That’s it! You have repetitive code that follows specific patterns. This repetition breaks the DRY (Don’t Repeat Yourself) principle, so you would want to refactor this code to avoid it. To do so, you can abstract out the repetitive logic using a descriptor that you can call Coordinate:

Now your code is a bit shorter and way less repetitive. You defined Coordinate as a descriptor to manage the data validation in a single place. Then, you create .x and .y as class attributes holding instances of the target descriptor. The code works just like its earlier implementation. Go ahead and give it a try!

In general, if you find yourself copying and pasting property definitions throughout your code or if you spot repetitive code, like in the example above, then you should consider using descriptors.

Providing Computed Attributes

If you need an attribute that builds its value dynamically whenever you access it, then using a property can be a great choice. These kinds of attributes are commonly known as computed attributes. They’re handy when you need something that works like an eager attribute, but you want it to be lazy.

The main reason for creating lazy attributes is to postpone their computation until the attributes are needed, which can make your code more efficient.

Here’s an example of how to use property() to create a computed attribute called .area in a Rectangle class:

In this example, the Rectangle initializer takes width and height as arguments and stores them in the corresponding instance attributes. The read-only property, .area, computes and returns the area of the current rectangle every time you access it.

Another cool use case of properties is to provide a formatted value for a given attribute:

In this example, .price is a property that formats and returns the price of a particular product. To provide a currency-like format, you use an f-string with an appropriate format specifier.

As a final example of computed attributes, say you have a Point class that uses .x and .y as Cartesian coordinates. You want to provide polar coordinates for your point so that you can use them in a few computations. The polar coordinate system represents each point using the distance to the origin and the angle with the horizontal coordinate axis.

Here’s a Cartesian coordinates Point class that also provides computed polar coordinates:

In this example, you define two properties to compute the polar coordinates—distance and angle—of a given Point object using its .x and .y Cartesian coordinates. You also add two instance methods that return the Cartesian and polar coordinates as tuples.

Here’s how this class works in practice:

Properties are handy tools for providing computed attributes. However, if you’re creating an attribute that you use frequently, then computing it every time can be costly and wasteful. A good strategy to avoid this additional cost is to cache the computed value once the computation is done. That’s what you’ll do in the following section.

Caching Computed Attributes

Sometimes you have a given computed attribute that you use frequently. Constantly running the same computation may be unnecessary and expensive. To work around this problem, you can cache the computed value for later reuse.

If you have a property that computes its value from constant input values, then the result will never change. In that case, you can compute the value just once:

This implementation of Circle caches the computed diameter using a dedicated non-public attribute. The code works, but it has the drawback that if you ever change the value of .radius, then .diameter won’t return a correct value:

In these examples, you create a circle with a radius equal to 42.0. The .diameter property computes its value only the first time you access it. That’s why you see a delay in the first execution and no delay in the second. When you change the value of the radius, the diameter stays the same, which is a problem.

If the input data for a computed attribute changes, then you need to recalculate its value:

The setter method of the .radius property resets ._diameter to None every time you change the radius. With this little update, .diameter recalculates its value the first time you access it after every mutation of .radius:

Cool! Circle works correctly now! It computes the diameter the first time you access it and also every time you change the radius.

Another way to create cached properties is to use functools.cached_property() from the standard library. This function works as a decorator and allows you to transform a method into a cached property. The property computes its value only once and caches it as a normal attribute during the lifetime of the instance:

Here, .diameter computes and caches its value the first time you access it. This kind of implementation is suitable for input values that don’t change. Here’s how it works:

When you access .diameter, you get its computed value. That value remains the same from this point on. However, unlike property(), cached_property() doesn’t block attribute updates unless you provide a setter method. That’s why you can update the diameter to 200 in the last couple of lines.

If you want to create a cached property that doesn’t allow modifications, then you can use property() and functools.cache() like in the following example:

This code stacks @property on top of @cache. The combination of both decorators builds a cached property that prevents changes:

In these examples, when you try to assign a new value to .diameter, you get an AttributeError because the setter functionality comes from the property’s internal descriptor.

Logging Attribute Access and Mutation

Sometimes, you need to keep track of what your code does and how your programs flow. A way to do that in Python is to use logging. This module provides all the functionality you require for logging your code. It allows you to constantly watch the code and generate useful information about how it works.

If you ever need to keep track of how and when you access and mutate a given attribute, then you can take advantage of property() for that, too:

Here, you first import logging and define a basic configuration. Then you implement Circle with a managed attribute .radius. The getter method generates log information every time you access .radius in your code. The setter method logs each mutation that you perform on .radius. It also logs those situations in which you get an error because of bad input data.

Here’s how you can use Circle in your code:

Logging data from attribute access and mutation can help you debug your code. Logging can also help you identify sources of problematic data input, analyze the performance of your code, spot usage patterns, and more.

Managing Attribute Deletion

You can create properties that implement deletion functionality. This might be a rare use case of property(), but having a way to delete an attribute can be handy in some situations.

Say you’re implementing your own tree data type. A tree is an abstract data type that stores elements in a hierarchy. The tree components are commonly known as nodes. Each node in a tree has a parent node, except for the root node. Nodes can have zero or more children.

Now, suppose you need to delete or clear the list of children of a given node. Here’s an example that implements a tree node that uses property() to provide most of its functionality, including the ability to clear the node’s list of children:

In this example, TreeNode represents a node in your custom tree data type. Each node stores its children in a Python list. Then, you implement .children as a property to manage the underlying list of children. The deleter method calls .clear() on the list of children to remove them all.

Here’s how your class works:

In this example, you first create a root node to initialize the tree. Then, you create two new nodes and assign them to .children using a list. The del statement triggers the internal deleter method of .children and clears the list of nodes.

Creating Backward-Compatible Class APIs

As you already know, properties turn direct attribute lookups into method calls. This feature allows you to create clean and Pythonic APIs for your classes. You can expose your attributes publicly without using getter and setter methods.

If you ever need to modify how you compute a given public attribute, then you can turn it into a property. Properties allow you to perform extra processing, such as data validation, without having to modify your public APIs.

Suppose you’re creating an accounting application and need a base class to manage currencies. To this end, you create a Currency class that exposes two attributes, .units and .cents:

This class looks clean and Pythonic. Now, say that your requirements change, and you decide to store the total number of cents instead of the units and cents. Removing .units and .cents from your public API to use something like .total_cents could break the code of more than one user.

In this situation, property() can be an excellent option to keep your current API unchanged. Here’s how you can work around the problem and avoid breaking your users’ code:

Now your class stores the total number of cents instead of independent units and cents. Because of the new properties, your users can still access and mutate .units and .cents in their code and get the same result as before. Go ahead and give it a try!

When you write code that others will build upon, you need to guarantee that modifications to your code’s internal implementation don’t affect how end users work with it.

Overriding Properties in Subclasses

When you create Python classes that include properties and distribute them in a package or library, you should expect your users to do unexpected things with them. One of those things could be subclassing them to customize their functionalities. In these cases, your users should be aware of a subtle gotcha. If you partially override a property, then you lose the non-overridden functionality.

For example, suppose you need an Employee class to manage employee information. You already have another class called Person, and you think of subclassing it to reuse its functionalities.

Person has a .name attribute implemented as a property. The current implementation of .name doesn’t work for Employee because you want the employee’s name to be in uppercase letters. Here’s how you may end up writing Employee using inheritance:

In Employee, you override .name to make sure that when you access the attribute, you get the employee name in uppercase:

Great! Employee works as you need and returns the name in uppercase letters. However, subsequent tests uncover an unexpected issue:

What happened? When you override an existing property from a parent class, you override the whole functionality of that property. In this example, you reimplemented the getter method only. Because of that, .name lost the rest of its inherited functionality. You don’t have a setter method any longer.

The takeaway here is that if you ever need to override a property in a subclass, then you must provide all the functionality you need in the new version of the property.

Conclusion

A property is a special type of class member that provides functionality that’s somewhere in between regular attributes and methods. Properties allow you to modify the implementation of instance attributes without changing the class’s public API. Being able to keep your APIs unchanged helps you avoid breaking code that your users have written on top of older versions of your classes.

Properties are the Pythonic way to create managed attributes in your classes. They have several use cases in real-world programming, making them a great addition to your skill set as a Python developer.

In this tutorial, you learned how to:

  • Create managed attributes with Python’s property()
  • Perform lazy attribute evaluation and provide computed attributes
  • Make your classes Pythonic using properties instead of setter and getter methods
  • Create read-only and read-write properties
  • Create consistent and backward-compatible APIs for your classes

You also wrote several practical examples that walked you through the most common use cases of property(). Those examples include input data validation, computed attributes, logging your code, and more.

Take the Quiz: Test your knowledge with our interactive “Python's property(): Add Managed Attributes to Your Classes” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python's property(): Add Managed Attributes to Your Classes

In this quiz, you'll test your understanding of Python's property(). With this knowledge, you'll be able to create managed attributes in your classes, perform lazy attribute evaluation, provide computed attributes, and more.

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Managing Attributes With Python's property()

Source: realpython.com

Related stories
1 week ago - In this tutorial, you'll learn how to harness the power of structural pattern matching in Python. You'll explore the new syntax, delve into various pattern types, and find appropriate applications for pattern matching, all while...
2 weeks ago - In this tutorial, you'll be guided step by step through the process of building a basic contact book application. You'll use Python and Textual to build the application's text-based user interface (TUI), and then use SQLite to manage the...
1 week ago - In this tutorial, you'll learn what syntactic sugar is and how Python uses it to help you create more readable, descriptive, clean, and Pythonic code. You'll also learn how to replace a given piece of syntactic sugar with another syntax...
3 weeks ago - SQLite is one of the most popular relational database management systems (RDBMS). It’s lightweight, meaning that it doesn’t take up much space on your system. One of its best features is that it’s serverless, so you don’t need to install...
1 month ago - Get a sneak peek at the upcoming features in Python 3.13 aimed at enhancing performance. In this tutorial, you'll make a custom Python build with Docker to enable free threading and an experimental JIT compiler. Along the way, you'll...
Other stories
7 minutes ago - Warehouse management software allows businesses to optimize their supply chain process, improve warehouse efficiency, and manage order fulfillment. With the rise of eCommerce merchants, global markets, and rising customer expectations,...
3 hours ago - One of the best things about the Raspberry Pi 5 (other than the performance boost over its predecessor) is how much easier it is to add an SSD. And using an SSD with the Raspberry Pi 5 is a no-brainer if you’re running a proper desktop OS...
5 hours ago - When you’re building a website, it’s important to make sure that it’s fast. People have little to no patience for slow-loading websites. So as developers, we need to use all the techniques available to us to speed up our site’s...
5 hours ago - In any software project, documentation plays a crucial role in guiding developers, users, and stakeholders through the project's features and functionalities. As projects grow and evolve, managing documentation across various...
5 hours ago - I've got a few pages here that are primarily built for my own use. One of them, my bots page, is a list of all the dumbsuper useful bots I've built for Mastodon (and Bluesky). The idea on this page is to show the latest post from each...