Python class Metaclass instance

Source: Internet
Author: User

>>> class CObj (object):
... pass
...
>>> dir ()
['cobj', ' __builtins__ ', ' __doc__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ']
>>> COB = CObj
>>> dir ()
[' CObj ', ' __builtins__ ', ' __doc__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', 'cob']

>>> Help (__name__)
Help on module __main__:

NAME
__main__

CLASSES
Builtins.object
CObj

Class CObj (Builtins.object)
| Data descriptors defined here:
|
| __dict__
| Dictionary for instance variables (if defined)
|
| __weakref__
| List of weak references to the object (if defined)

COB = Class CObj (builtins.object)
| Data descriptors defined here:
|
| __dict__
| Dictionary for instance variables (if defined)
|
| __weakref__
| List of weak references to the object (if defined)

FILE
(built-in)


>>> cins = COB ()
>>> Cins
<__main__. CObj object at 0x0000000000b5cc88>
>>> cins2 = CObj ()
>>> Cins2
<__main__. CObj object at 0x00000000013f5e10>
>>> cobj.x1 = 20
>>> Cins
<__main__. CObj object at 0x0000000000b5cc88>
>>> cins.x1
20
>>> cins2.x1
20
>>> cins2.y1=30
>>> cins.y1
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' CObj ' object has no attribute ' y1 '
>>> cobj.y1
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror:type object ' CObj ' has no attribute ' y1 '
>>> dir (cins)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' _ _
Gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __
Repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' x1 '
>>> dir (cins2)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' _ _
Gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __
Repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' x1 ', ' y1 '
>>> dir (COB)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' _ _
Gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __
Repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' x1 '
>>> dir (cobj)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' _ _
Gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __
Repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' x1 '
>>>

>>> hasattr (CObj, X1)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' x1 ' is not defined
>>> hasattr (cobj, ' X1 ')
True
>>> hasattr (cobj, ' Y1 ')
False
>>> hasattr (cobj, ' Y1 ')

Python class Metaclass instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.