15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

isinstance(c2, C2)<br />

True<br />

>>> isinstance(C2, c2)<br />

Traceback (innermost last):<br />

File "", line 1, in ?<br />

isinstance(C2, c2)<br />

TypeError: second argument must be a class<br />

Note that the second argument should be a class; otherwise, you get a TypeError. The only exception is<br />

if the second argument is a type object. This is allowed because you can also use isinstance() to check<br />

if an object obj1 is of the type obj2, i.e.,<br />

>>> isinstance(4, int)<br />

True<br />

>>> isinstance(4, str)<br />

False<br />

>>> isinstance('4', str)<br />

True<br />

If you are coming from Java, you may be aware of the warning against using its equivalent, instanceof<br />

(), due to performance reasons. A call to <strong>Python</strong>'s isinstance() will not have the same performance hit<br />

primarily because it only needs it to perform a quick search up the class hierarchy to determine what<br />

classes it is an instance of, and even more importantly, it is written in C!<br />

Like issubclass(), isinstance() can also take a tuple as its second argument. This feature was added in<br />

<strong>Python</strong> 2.2. It will return TRue if the first argument is an instance of any of the candidate types and<br />

classes in the given tuple. Also be sure to read more about isinstance() in Section 13.16.1 on page 595.<br />

13.12.3. hasattr(), getattr(), setattr(), delattr()<br />

The *attr() functions can work with all kinds of objects, not just classes and instances. However, since<br />

they are most often used with those objects, we present them here. One thing that might throw you off<br />

is that when using these functions, you pass in the object you are working on as the first argument, but<br />

the attribute name, the second argument to these functions, is the string name of the attribute. In other<br />

words, when operating with obj.attr, the function call will be like *attr(obj, 'attr'...)this will be clear<br />

in the examples that follow.<br />

The hasattr() function is Boolean and its only purpose is to determine whether or not an object has a<br />

particular attribute, presumably used as a check before actually trying to access that attribute. The<br />

getattr() and setattr() functions retrieve and assign values to object attributes, respectively. getattr<br />

() will raise an AttributeError exception if you attempt to read an object that does not have the<br />

requested attribute, unless a third, optional default argument is given. setattr() will either add a new<br />

attribute to the object or replace a pre-existing one. The delattr() function removes an attribute from<br />

an object.<br />

Here are some examples using all the *attr() BIFs:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!