python built-in functions --- hasattr, setattr, getattr

1. Description

the hasattr () function for determining whether the object contains the corresponding property.

grammar

hasattr syntax:

hasattr(object, name)


2, Description

setattr () function corresponding to the function getattr () , is used to set the property value, the property is not always present.

grammar

setattr () syntax:

setattr(object, name, value)

parameter

  • object - the object.
  • name - String object properties.
  • value - the property value.
 

3, description

getattr () function returns the value of an object attribute.

grammar

getattr syntax:

getattr(object, name[, default])

parameter

  • object - the object.
  • name - String object properties.
  • default - default return value, if the parameter is not available, when there is no corresponding attribute triggers AttributeError.

return value

Returns the object property value.

Examples

The following example shows getattr use:

>>> class A ( Object ) : ... bar = . 1 ... >>> A = A ( ) >>> getattr ( A , ' bar ' ) # Get the value of the attribute bar . 1 >>> getattr ( A , ' bar2 ' ) property bar2 absent, an exception is 








Guess you like

Origin www.cnblogs.com/1314520xh/p/11569765.html