Discovering objects and class attributes in Python
You may have heard that in python everything is object and everything is an instance of some class.
A Class
In Python a class is a code template to create objects, you can use the keyword class
to creat it. Objects have member variables and have behaviour associated with them.
Creating a Class
The simplest class can be created using the class keyword. For example, let’s create a simple, empty class with no functionalities.
Methods and Attributes in classes
A class by itself is of no use unless there is some functionality associated with it. Functionalities are defined by setting attributes, which act as containers for data and functions related to those attributes. Those functions are called methods.
Attributes
You can define the following class with the name Snake. This class will have an attribute name
.
>>> class Snake:
... name = "python" # set an attribute `name` of the class
...
You can assign the class to a variable. This is called object instantiation. You will then be able to access the attributes that are present inside the class using the dot .
operator. For example, in the Snake example, you can access the attribute name
of the class Snake
.
Methods
Once there are attributes that “belong” to the class, you can define functions that will access the class attribute. These functions are called methods. When you define methods, you will need to always provide the first argument to the method with a self keyword.
For example, you can define a class Snake
, which has one attribute name
and one method change_name
. The method change name will take in an argument new_name
along with the keyword self
.
>>> class Snake:
... name = "python"
...
... def change_name(self, new_name): # note that the first argument is self
... self.name = new_name # access the class attribute with the self keyword
...
Now, you can instantiate this class Snake
with a variable snake
and then change the name with the method change_name
.
>>> # instantiate the class
>>> snake = Snake()
>>> # print the current object name
>>> print(snake.name)
python
>>> # change the name using the change_name method
>>> snake.change_name("anaconda")
>>> print(snake.name)
anaconda
Instance attributes in python and the init method
You can also provide the values for the attributes at runtime. This is done by defining the attributes inside the init method.
class Snake:
def __init__(self, name):
self.name = name
def change_name(self, new_name):
self.name = new_name
Now you can directly define separate attribute values for separate objects. For example,
>>> # two variables are instantiated
>>> python = Snake("python")
>>> anaconda = Snake("anaconda")
>>> # print the names of the two variables
>>> print(python.name)
python
>>> print(anaconda.name)
anaconda
Class vs. Instance Attributes
Some difference in class and attributes, are the “shared instances”, if you change the valuo in the class attribute, this affect all the instances that have the value . The attribute of an instance on the other hand is unique to that instance.
Do you know about __dict__?
Is a special attribute of every module is __dict__. This is the dictionary containing the module’s symbol table.
object.__dict__
A dictionary or other mapping object used to store an object’s (writable) attributes.
class MyClass(object):
class_var = 1
def __init__(self, i_var):
self.i_var = i_var
foo = MyClass(2)
bar = MyClass(3)
print MyClass.__dict__
print foo.__dict__
print bar.__dict__
Output:
{'__module__': '__main__', 'class_var': 1, '__dict__': <attribute '__dict__' of 'MyClass' objects>, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': None, '__init__': <function __init__ at 0x0000000004E55CF8>}
{'i_var': 2}
{'i_var': 3}
Pythonic vs No Pythonic
No Pythonic
class Square:
def __init__(self, size=0):
if not isinstance(size, int):
raise TypeError("size must be an integer")
if size < 0:
raise ValueError("size must be >= 0")
self.__size = size * size def area(self):
return self.__sizet__width()
Pythonic
class Square:
def __init__(self, size=0):
self.size = size @property
def size(self):
return self.__size @size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
if value < 0:
raise ValueError("size must be >= 0")
self.__size = value def area(self):
return self.__size * self.__size
Sources: