1.1 Get object Information1.1.1 UseType ()Judging Object Types
>>> type (123)-- basic data type judgment
<class ' int ' >
>>> type (' 123 ')
<class ' str ' >
>>> type (ABS)--python built-in function judgment
<class ' Builtin_function_or_method ' >
>>> type (DOG)
<class ' type ' >
>>> type (Dog ())--The judgment of the custom class, quoted in the Dogclass above
<class ' __main__. Dog ' >
>>> type (123) = = Type (456)
True
>>> type (' 123 ') = = Type (' 456 ') # can be judged in the if
True
determines whether an object is a function, Types constants in the module
>>> importtypes
>>> def fn ():
... pass
...
>>> type (fn) = = types. Functiontype
True
>>> type (ABS) ==types. Builtinfunctiontype
True
>>> type (lambda x:x) ==types. Lambdatype
True
>>> type ((x for x in range) = = types. Generatortype
True
1.1.2 UseIsinstance ()Judging Inheritance
>>> class Animal (object):
... pass
...
>>> class Dog (Animal):
... pass
...
>>> class Erha (DOG):
... pass
...
>>> # Erha inheritance relationship Animal->dog
... a = Animal () # Create an object in turn
>>> B = Dog ()
>>> C = Erha ()
>>>
>>> isinstance(C,ERHA)
True
>>> isinstance (c, Dog)
True
>>> isinstance (c, Animal)
True
>>> Isinstance (b, Dog)
True
>>> Isinstance (b, Animal)
True
>>> Isinstance (b, Erha)
False
can be judged by Type() or by isinstance
>>> Isinstance (123, Int.)
True
>>> isinstance (' a ', str)
True
>>> isinstance ([3], (list, tuple)) -- determine if a variable is one of some types
True
>>> Isinstance ((1, 2, 3), (List,tuple))
True
1.1.3 UseDir ()Get properties and methods for an object
>>> dir (' abc ')-- list properties and methods
[' __add__ ', ' __class__ ', ' __contains__ ', ' __delattr__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __ getattribute__ ', ' __getitem__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' _ _lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rmod__ ', ' __rmul__ ', ' _ _setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' capitalize ', ' casefold ', ' center ', ' count ', ' encode ', ' EndsWith ', ' expandtabs ', ' find ', ' format ', ' Format_map ', ' index ', ' isalnum ', ' isalpha ', ' isdecimal ', ' isdigit ', ' Isidentifier ', ' islower ', ' isnumeric ', ' isprintable ', ' isspace ', ' istitle ', ' isupper ', ' join ', ' ljust ', ' lower ', ' Lstrip ' ', ' Maketrans ', ' partition ', ' replace ', ' rfind ', ' rindex ', ' rjust ', ' rpartition ', ' rsplit ', ' Rstrip ', ' Split ', ' Splitlines ', ' startswith ', ' strip ', ' swapcase ', ' title ', ' Translate ', ' upper ', ' Zfill ']
Mates getattr () , setattr () as well hasattr () , we can manipulate the state of an object directly.
>>> class Myobject (object):
... def __init__ (self):
... self.x = 9
... def power (self):
... return self.x * self.x
...
>>> obj = Myobject ()
>>> hasattr (obj, ' x ') # has attribute ' x '?
True
>>> obj.x
9
>>> getattr (obj, ' x ')
9
>>> hasattr (obj, ' y ') # has attribute ' y '?
False
>>> setattr (obj, ' y ', +) # sets a Y property of 19
>>> getattr (obj, ' y ')
19
>>> OBJ.Y
19
>>> hasattr (obj, ' power ') # have attribute Power(method)?
True
>>> getattr (obj, ' power ') # get Property Power
<bound method Myobject.power of<__main__. Myobject Object at 0x2af174d0bd68>>
Syntaxerror:invalid character Inidentifier
>>> getattr (obj, ' power ') () # get property Power value
81
>>> fn = getattr (obj, ' power ')
>>> fn ()
81
Hasattr 's classic application scenario
Apply the above Myobject () instance
>>> def readpower (obj):
... if hasattr (obj, ' power '):
... return obj.power ()
.. else:
... return None
...
>>> Readpower (Myobject)
Traceback (most recent):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in Readpower
Typeerror:power () Missing 1 requiredpositional argument: ' Self '
>>> Myobject (). x
9
>>> Myobject (). Power ()
81
>>> Readpower (Myobject ())
81
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1826206
Python Object-oriented programming--Get object information