|Programmer's Python - Variables, Objects and Attributes|
|Written by Mike James|
|Monday, 02 April 2018|
Page 2 of 2
Once you have a reference to an object you can generally add attributes to it simply by assigning to them.
creates myNewAttr and sets it to the value 2.
In fact, you can create a complete custom object by first creating an object with no attributes and then adding them as required.
and so on. In case you haven’t met it before, pass is the ultimate do nothing instruction in Python. You can use it as placeholder or anywhere an instruction would normally be expected.
Just as it is possible to add attributes you can also remove them using the del command.
removes myAttribute2 from MyObject.
If you need to delete an attribute that is determined at run time you can use the delattr function:
where obj and name are variables.
If you want to know what attributes an object currently has you can use the dir function:
As you can see, a simple object created using the class built-in object has a lot of attributes that you didn’t create. This is because it isn’t a simple custom object in Python; it has a higher purpose which we'll examine in a later chapter.
Each attribute in an object can reference another object and any attributes in it can reference other objects. That is, nested objects are the norm in Python, although you generally don’t see explicit examples in practice.
This looks odd but it works perfectly and makes sense if you think of it as:
This isn’t correct syntax but it makes it look much more like an attribute definition.
Once you have a nested class you have to use a fully qualified name to access the attribute.
As this is just a reference you can use a new variable to simplify the name.
You can always choose to save a reference in a simpler variable to create a shorter name and this is a general principle in Python.
Namespaces and Modules
At this point objects don’t look particularly useful. An object is just a collection of attributes that you can access and attributes are just like variables that you have to access using a qualified name.
This system of qualified names is central to Python and it is part of the object system it implements. When you create an object you also create a namespace. If you know about namespaces then it is worth saying that Python doesn’t quite do it in the same way as other languages. Each object has a Dictionary, i.e. a Python Dictionary data structure, that it uses to store names defined within the object and what they reference.
has a Dictionary, which has as an entry myAttribute with the value 1. When you use an instruction like:
what happens is that the Dictionary belonging to the object that MyObject references is used to lookup myAttribute. If it finds it, then the value is retrieved and used. Clearly myAttribute defined on the object referenced by MyObject has nothing to do with any other attribute or variable defined in any other object, which is of course the purpose of namespaces.
So it looks as if each object you create makes its own namespace, but notice that because objects don’t have fixed names the prefix can vary.
All variables in Python are part of the namespace, i.e. the Dictionary of the object they are defined in.
What about variables defined at the top level of a program, i.e. not within an object?
The answer to this is that they are within an object – a module object.
A module is a file of code that you can load into the Python system and run. The file has a name that ends in .py and the module has the same name as the file, but without the ,py. The module has a Dictionary and it stores all of the variables defined in the module not within an object, i.e. the global variables.
When you are working within a module the modules’ Dictionary is used by default, but when you load a module into another program you have to use the module’s name as a prefix.
Notice that if there is any executable code, rather than just definitions, then that code is executed as the module is read in.
For example, if you create and save the a file called MyModule.py containing:
you can load this module into another module using:
This loads the module and its Dictionary. After this you can refer to variables in the module using a qualified name. For example:
If you want to access an attribute on an object you simply use the fully qualified name:
and so on.
Modules behave like objects with attributes consisting of all of the global variable in the module.
Viewed in this way you can see everything really is an object. Even the program that you write and save in a file is a module, which behaves like an object with attributes that are its global variables.
This is also the sense in which there are no global variables, in Python just attributes of modules.
The module idea is simple and straightforward, but Python also provides ways for you to modify the way names are made available when you import a module. Notice that none of this gives you anything new, it is just a way of avoiding have to write out fully qualified names.
You can import specific objects within the module and you can change the name of the import by using the as qualifier.
imports just the object specified and
imports the same object, but changes the variable that references it to MySecondObj.
If you want to get rid of the qualified names you can use from.
imports just myModule.MyObject and references it with a variable called MyObject, i.e. no qualified name needed.
You can also change the name of the variable:
imports just MyObject and changes the variable to MySecondObj.
Finally, you can use a wildcard character to import everything in a module without changing any variable names:
This isn’t recommended because of the possibility of name clashes.
Notice that in all cases changing the name of an imported attribute means that the original reference to that attribute isn’t imported.
That is, after:
you have to refer to MyObject and not myModule.MyObject.
It is also worth noting that a module is only loaded once, no matter how many import statements you use. If you really need to reload a module use the reload function.
There are many variations on importing modules and there is a larger unit called a package which consists of multiple modules. However, the key point is that the idea of the module is part of the everything is an object idea and of the every object has a namespace provided by a Dictionary.
Namespace Access – Advanced Topic
One of the things that is amazing about Python is the way that most of its internal structure is implemented using Python. For example, the namespaces system is implemented using Dictionary objects. What is even more surprising is that the language provides access to most of these features. In particular, you can access the Dictionary object that each object, including a module, has.
Every object has a __dict__ attribute that is its local namespace i.e. it records all of the object's attributes. You can read the namespace and in some cases you can modify it, although it is considered bad practice to do so. In the case of class objects you cannot assign to the __dict__ attribute.
If you try:
You will see a listing of everything that its Dictionary contains:
Internally an attribute is looked up in the object’s dictionary. That is:
is equivalent to:
It isn’t often that modifying the Dictionary is useful but it can be useful to check what attributes are defined on an object.
Generally speaking built-in objects don’t allow you to modify their Dictionary it is too dangerous. There are times, however, when being able to change and write new entries in a Dictionary is useful.
Perhaps the most important object in all of Python is the function object and yet it is possible to learn to write effective code without ever realizing that it is indeed an object.
|Last Updated ( Wednesday, 27 February 2019 )|