Graphics Reference
In-Depth Information
>>> p = Person()
After assigning the object to the variable
p
, you can access information from
p
by calling
p.get_eye_color()
.Thesyntaxhereisimportant.Callingamethodonitsobjectisdonebyappendingthe
method name onto the end of the object name, separated by a dot. The method name ends with curly brackets
containing whatever arguments are appropriate for the method. If no arguments are necessary, the brackets are
empty.Whenthismethodiscalledforthefirsttime,thestring
Green
isprintedout,becauseitisdefinedasthe
default value in the class definition.
>>> p.get_eye_color()
Green
Attribute values are also accessed by appending the value name to the end of the object name, separated by
a dot. The next line assigns the string
"Blue"
to the
eye_color
attribute for
p
. After this new value is as-
signed to the attribute and
p.get_eye_color()
is called a second time, the string
Blue
is printed.
>>> p.eye_color = "Blue"
>>> p.get_eye_color()
Blue
In the API, you will find a large collection of descriptions of classes. Each class description also includes a
description of the kinds of attributes and methods that are appropriate for interacting with objects of that class.
AfteryougetusedtolookingthingsupintheAPI,theinformationshouldbecomemostlyself-explanatorymost
of the time. The next few chapters discuss some areas that might not be quite so obvious, to give you a sense of
whatkindsofinconsistenciestobeonthelookoutfor.BecomingconversantwiththewaytheAPIisintendedto
work will make it much quicker to get to the bottom of the problem on the occasions when things are not quite
as they should be.
The
dir()
Function
A useful function that will save you tons of time looking things up in the API is
dir()
. By using this function
with an object name as the argument, you can return a list of methods and attributes defined for the class of any
object.BecauseinPythoneverythingisanobject,thismeansyoucancall
dir()
onanydatayouwantto,even
on methods themselves, and get a list of all the appropriate methods for that class. If you call
dir()
on the
p
variable representing a person from the previous section, a list will be returned like this:
>>> dir(p)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'eye_color',
'get_eye_color']
Most of the methods listed here are standard for all classes and hold various information about the module
in which they are defined. The last two elements in the list are the attribute and method you defined previously:
eye_color
and
get_eye_color
, respectively.
Another useful source of information is the
__doc__
attribute, which returns a documentation string when
called from objects for which it's been defined. Most built-in classes have defined
__doc__
attributes. To see
the documentation string for integers, try calling it from an
int
object, like this: