CLASS & INSTANCE ATTRIBUTES IN PYTHON

CLASS & INSTANCE ATTRIBUTES IN PYTHON

Python is an object oriented programming language, almost everything in Python is an object, with its properties and methods. For instance, an object could represent a person with name, age and addres as "properties" and behaviors such as walking, talking, etc.

Classes

A class is like an object constructor. They are used to create user-defined data structures. Classes define functions called methods. The objective is to set up the behaviors and actions that an object created from the class can perform with its data.

Instances

An instance is an object that is built from a class and contains data, many instances can be created from a single class.

Class attributes VS Instance attributes

A class attribute is a Python variable that belongs to an entire class. It is shared between all the objects of the class, this means every object is able to use this variable. This kind of attribute is defined outside the constructor. In the other hand, an instance attribute is a Python variable belonging to one specific object and thus it is only accessible in the scope of it object. Contrary to the class attributes, instance attributes are defined inside the constructor function.

No hay texto alternativo para esta imagen

Differences

No hay texto alternativo para esta imagen

Advantages & Disadvantages

The advantages of class attributes are that all instances of the class inherit the attribute defined in the class. They also can store data that is relevant to all the instances. As a disadvantage we can say that it can get messy when we create an instance in which the value of the class attribute is different than the class attribute value. Here, when we try to retrieve the value through another instance, the behavior becomes unexpected. Another disadvantage could be that it is not posible to have two instances with different values.

Talking about instance attributes, they have as advantage that they are specific to an object and are easy to set and get. They are also discarded once the instance is deleted, this can be considered as an advantage (because makes things much clearer) or a disadvantage (as an example, in some cases you may want to keep history of values but this property makes it really hard because of the lost of the values). Another disadvantage is that they dont allow keeping track of values between instances.

__dict__

All objects in Python have an attribute __dict__, which is a dictionary object containing all attributes defined for that object itself. Because all methods belong to a class, they are also stored in this dictionary.

要查看或添加评论,请登录

Matías Martínez的更多文章

  • Libraries in C

    Libraries in C

    What are libraries? First of all, libraries are a collection of header files, so, what are header files? In C, header…

  • Hard and Symbolic links on Linux

    Hard and Symbolic links on Linux

    Hard Links: A hard link is a file that points to the same underlying inode, as another file. In case you delete one…

  • Compiling a C program

    Compiling a C program

    ABOUT C C is a compiled computer programming language that provides constructs that map efficiently typical machine…

社区洞察

其他会员也浏览了