• No results found

Using Built-In Functions

In document Making Use of Python (Page 197-200)

You have already discussed and used some built-in functions, such as dir() and var(). Let’s now look at the syntax of some other common built-in functions available in Python for OOP and learn how to use them.

isinstance(object1, object2)

The isinstance() function takes two arguments, in which the first argument is an instance object and the second argument is a class object or a type object, for example, object1and object2.

Let’s consider an example to explain the isinstance() function. Continuing with the same example that was used to explain inheritance, My_Base_Class already has an instance, b, and My_Subclass has an instance, s. Let’s create instances for My_Class_Aand My_Class_B.

>>>classA = My_Class_A >>>

>>>classB = My_Class_B

In the isinstance() function, if the second argument is a class, the function deter- mines whether object1 is an instance of the class object2. If this is true, the func- tion returns 1.

>>>isinstance(classA, My_Class_A) 1

If object1 is not an instance of the class object2, the function returns 0.

>>>isinstance(classA, My_Class_B) 0

In the isinstance() function if the second argument is a type object, the function determines whether object1 is of the type object2. If this is true the function returns 1. You use the type() function to determine the type of any object. In Python, the standard types need not be classes and they cannot be used for direct derivation. All the built-in standard types of Python are defined in the types standard module.

>>>isinstance(‘a’, type(‘z’)) 1 >>> >>>type(‘a’) <type ‘str’> >>> >>>type(‘z’) <type ‘str’>

Here, both ‘a’ and ‘z’ are strings and are of the type str. Hence, the isinstance()function returns 1.

If object1 is not of the type object2, the isinstance() function returns 0. >>>isinstance(‘a’, type(1)) 0 >>> >>>type(1) <type ‘int’>

Here, 1 is an integer and is of the type int, while ‘a’ is a string and is of the type str. Hence, the isinstance() function returns 0.

If object2 is not a class object or a type object, the function raises a TypeError exception.

>>>isinstance(‘a’, classA)

Traceback (most recent call last): File “<pyshell#49>”, line 1, in ?

isinstance(‘a’, classA)

TypeError: isinstance() arg 2 must be a class or type

Here, classA is not a class object or a type object. Hence, the isinstance() func- tion raises a TypeError exception.

issubclass(class1, class2)

issubclass()takes two arguments, and both of them are classes—for example, class1and class2.

It determines whether class1 is a subclass of class2, and if this is true, the func- tion returns 1. Let’s take the same example that was used to explain inheritance.

>>>issubclass(My_Class_B, My_Base_Class) 1

Here, My_Base_Class is the parent class of My_Class_B. If class1 is not a subclass of class2, the function returns 0.

>>>issubclass(My_Subclass, My_Class_B) 0

Here, My_Class_B is not the parent or ancestor class of My_Subclass.

If class2 is an ancestor of class1, the result will be true and the function will return 1.

>>>issubclass(My_Class_B, My_Subclass) 1

My_Class_Bis inherited from My_Class_A, and My_Class_A is inherited from My_Subclass, thus making My_Subclass the ancestor class of My_Class_B.

Also, if both class1 and class2 are the same, the function returns 1 because a class is considered a subclass of itself.

>>>issubclass(My_Class_B, My_Class_B) 1

But, if one or both the arguments of the issubclass() function are not class objects, a TypeError exception is raised.

>>>issubclass(classB, My_Class_B) Traceback (most recent call last):

File “<pyshell#53>”, line 1, in ? issubclass(classB, My_Class_B)

TypeError: issubclass() arg 1 must be a class

Here, classB is not a class object but an instance object. Hence, the function raises a TypeError exception.

hasattr(obj, attr)

The hasattr() function takes two arguments, in which the first argument is an object (for example, obj) and the second argument is a string (for example, attr). It determines whether the string is the name of one or more of the object attributes. This function can be used to check whether the object attributes that you want to refer to actually exist.

Let’s consider an example to explain the hasattr() function. Let’s create a new class, My_Attr_Class, and define two class attributes.

>>>class My_Attr_Class: a=0

b=1

If attr is the name of an attribute of the object, obj, the function returns 1.

>>>hasattr(My_Attr_Class, ‘a’) 1

Here, a is an attribute of the class, My_Attr_Class. Hence, the function returns 1. If attr is not the name of an attribute of the object, obj, the function returns 0.

>>>hasattr(My_Attr_Class, ‘z’) 0

Here, z is not an attribute of the class, My_Attr_Class. My_Attr_Class has only two attributes, a and b. Hence, the function returns 0.

getattr(obj, attr)

The getattr() function takes two arguments, in which the first argument is an object (for example, obj) and the second argument is a string (for example, attr). It returns the value of the attribute that has the same name as the string.

If attr is the name of an attribute of the object, obj, the function returns the value of the attribute.

>>>hasattr(My_Attr_Class, ‘a’) 0

Here, a is an attribute of the class, My_Attr_Class, and its value is 0.

If attr is not the name of an attribute of the object, obj, the function raises an AttributeErrorexception.

>>> getattr(My_Attr_Class, ‘z’) Traceback (most recent call last):

File “<pyshell#46>”, line 1, in ? getattr(My_Attr_Class, ‘z’)

AttributeError: class My_Attr_Class has no attribute ‘z’

Here, z is not an attribute of the class, My_Attr_Class. My_Attr_Class has only two attributes, a and b. Hence, the function raises an AttributeError exception.

setattr(obj, attr, val)

The setattr() function takes three arguments, in which the first argument is an object (for example, obj), the second argument is a string (for example, attr), and the third argument is a value (for example, val). You use this function to change the value of an existing attribute or set a new attribute for an object.

When you use the setattr() function to change the value of an existing attribute, it assigns the value, val, to the attribute, attr, of the object, obj, or it sets a new attribute, attr, in the object, obj, and assigns the value, val.

Let’s update the value of the attribute, a, of the class, My_Attr_Class, from 1 to 10.

>>>setattr(My_Attr_Class, ‘b’, 10) >>>

>>>getattr(My_Attr_Class, ‘b’) 10

When you use the setattr() function to set a new attribute, it sets a new attribute, attr, in the object, obj, and assigns the value, val. Let’s consider an example. Let’s first list the present attributes of the class, My_Attr_Class, before the new attribute is added.

>>>dir(My_Attr_Class)

[‘__doc__’, ‘__module__’, ‘a’, ‘b’]

My_Attr_Class has only two attributes, a and b, besides the standard built-in attributes.

Let’s now set a new attribute, c, with the value as 100 in the class, My_Attr_Class.

>>>setattr(My_Attr_Class, ‘c’, 100) >>>

>>>getattr(My_Attr_Class, ‘c’) 100

Let’s now list the attributes of My_Attr_Class to confirm that the new variable, c, has been set with the value, 100.

In document Making Use of Python (Page 197-200)