Difference between revisions of "Attribute"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(Created page with "== Class vs. instance attributes == == @property == == AttributeErrors == === 'Nonetype' has no attribute ===")
 
(Added a summary)
Line 1: Line 1:
 +
An '''attribute''' of an object is a piece of data that describes the object, stored within the object as a key-value pair. Given an object, one can ask for the value of a particular key using ''dot notation''.
 +
<syntaxhighlight lang="python">
 +
>>> object.key
 +
value
 +
</syntaxhighlight>
 +
To access an object's list of attributes, just call the <code>dir()</code> function on the object.
 +
 +
== Example of handling attributes ==
 +
<syntaxhighlight lang="python">
 +
>>> import datetime
 +
>>> date_object = datetime.date
 +
>>> dir(date_object)
 +
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', ..., 'today', 'toordinal', 'weekday', 'year']
 +
>>> date.year
 +
2014
 +
</syntaxhighlight>
 
== Class vs. instance attributes ==
 
== Class vs. instance attributes ==
  
Line 4: Line 20:
  
 
== AttributeErrors ==
 
== AttributeErrors ==
 +
When one tries to access an attribute of the object that does not exist, Python will raise an AttributeError: 'type_of_obj' object has no attribute 'key'.
 
=== 'Nonetype' has no attribute ===
 
=== 'Nonetype' has no attribute ===
 +
This error occurs when the object to the left of the dot notation is <code>None</code>.
 +
<syntaxhighlight lang="python">
 +
>>> class Foo():
 +
...    def bar(self):
 +
...        print(3)
 +
...
 +
>>> a = Foo()
 +
>>> b = a.bar()
 +
3
 +
>>> b.bar()
 +
Traceback (most recent call last):
 +
  File "<stdin>", line 1, in <module>
 +
AttributeError: 'NoneType' object has no attribute 'bar'
 +
</syntaxhighlight>
 +
== Naming conventions ==
 +
* _single_leading_underscore indicates that the attribute should be only be used internally by the object.
 +
* __double_leading_and_trailing__underscores__ denote [[Magic method|"magic" attributes]], built-in attributes in Python that have a particular meaning beyond just holding a key-value pair.
 +
 +
 +
== Sources ==
 +
* [http://legacy.python.org/dev/peps/pep-0008 PEP 8 Style Guide for Python Code]

Revision as of 14:13, 6 July 2014

An attribute of an object is a piece of data that describes the object, stored within the object as a key-value pair. Given an object, one can ask for the value of a particular key using dot notation.

>>> object.key
value

To access an object's list of attributes, just call the dir() function on the object.

Example of handling attributes

>>> import datetime
>>> date_object = datetime.date
>>> dir(date_object)
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', ..., 'today', 'toordinal', 'weekday', 'year']
>>> date.year
2014

Class vs. instance attributes

@property

AttributeErrors

When one tries to access an attribute of the object that does not exist, Python will raise an AttributeError: 'type_of_obj' object has no attribute 'key'.

'Nonetype' has no attribute

This error occurs when the object to the left of the dot notation is None.

>>> class Foo():
...     def bar(self):
...         print(3)
...
>>> a = Foo()
>>> b = a.bar()
3
>>> b.bar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'bar'

Naming conventions

  • _single_leading_underscore indicates that the attribute should be only be used internally by the object.
  • __double_leading_and_trailing__underscores__ denote "magic" attributes, built-in attributes in Python that have a particular meaning beyond just holding a key-value pair.


Sources