Book Image

Mastering Object-oriented Python

By : Steven F. Lott, Steven F. Lott
Book Image

Mastering Object-oriented Python

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (26 chapters)
Mastering Object-oriented Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Some Preliminaries
Index

Basic attribute processing


By default, any class we create will permit the following four behaviors with respect to attributes:

  • To create a new attribute by setting its value

  • To set the value of an existing attribute

  • To get the value of an attribute

  • To delete an attribute

We can experiment with this using something as simple as the following code. We can create a simple, generic class and an object of that class:

>>> class Generic:
...     pass
...     
>>> g= Generic()

The preceding code permits us to create, get, set, and delete attributes. We can easily create and get an attribute. The following are some examples:

>>> g.attribute= "value"
>>> g.attribute
'value'
>>> g.unset
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Generic' object has no attribute 'unset'
>>> del g.attribute
>>> g.attribute
Traceback (most recent call last):
  File "<stdin>", line 1, in <module&gt...