A.I, Data and Software Engineering

Advanced Python: classes String and computed attribute

A

This article demonstrates more advanced features of Python classes and their customisation. Specifically, how enum objects are created, class to strings, and computed attribute.

Class string values

A string is typically an informal representation of the object. However, it’s just a nicely in human-readable form. Python provides 2 common methods, i.e. str() and repr(). We can customise the two built-in methods to display the information that we want from an object.

Now if we create a Person object and print it out using these functions:

We will have the following output:

str() and repr() both are used to get a string representation of an object. But they have the following differences:

  • str() is used for creating output for end user while repr() is mainly used for debugging and development. repr’s goal is to be unambiguous and str’s is to be readable. For example, if we suspect a float has a small rounding error, repr will show us while str may not.
  • repr() compute the “official” string representation of an object (a representation that has all information about the object) and str() is used to compute the “informal” string representation of an object (a representation that is useful for printing the object).
  • The print statement and str() built-in function uses __str__ to display the string representation of the object while the repr() built-in function uses __repr__ to display the object.
reprstr
s = 'Hello, Petamind.'
print(repr(s))
print(repr(2.0/11.0))
s = 'Hello, Petamind.'
print(str(s))
print(str(2.0/11.0))
Hello, Petamind.
0.181818181818
‘Hello, Petamind.’
0.18181818181818182
repr vs str

Defining enumerations

Python supports enumerations just like other popular programming languages and they’re useful in a variety of scenarios. Usually, they are used to assign easy-to-read names to constant values in a program. Also, you can iterate over them like you would other iterable in Python. 

We define enumerations using the class syntax. In the following code, we create an enum type Fruit of unique value with @unique annotation. We can use auto() function to automatically assign values to enums.

Enums have human-readable values and types. Let print their values and types:

Enums are hashable so we can use them as unique keys of a dictionary.

Computed ATTRIBUTES

Python provides a set of methods that classes can use to access the attributes of an object. Whenever we retrieve or set an object’s attributes, Python calls one of these functions to perform any desired processing. 

The first two, getattribute and getattr, are called to retrieve an attribute value. These are slightly different from each other. Getattr is called only when the requested attribute can’t be found on the object. Meanwhile, getattribute is called unconditionally every time an attribute name is requested. Additionally, there are setattrdelattr, and dir to set, delete and discover supported attributes.

The above code modifies the attribute related methods to get colour information. As user passes a supported attribute name, e.g. rgbcolor, python will return the value corresponding to the name.

Click here for other advanced python topics.

Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest

Newsletters

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.