• python class metaclass instance


    >>> 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 call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'CObj' object has no attribute 'y1'
    >>> CObj.y1
    Traceback (most recent call last):
    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 call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'x1' is not defined
    >>> hasattr(CObj, 'x1')
    True
    >>> hasattr(CObj, 'y1')
    False
    >>> hasattr(CObj, 'y1')

  • 相关阅读:
    ASP.NET Core2利用MassTransit集成RabbitMQ
    ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持wordexcelpptpdf等格式)
    ASP.NET Core2利用Jwt技术在服务端实现对客户端的身份认证
    net core System.Drawing is not supported on this platform.
    小程序开发之维护access_token
    net core 100个案例
    msgSystem.Drawing is not supported on this platform【netcore】
    wpf,前端动画demo,鱼眼效果
    自定义控件,重写 TextBox 实例
    TextBox输入法控制,进入输入框则启用或禁用输入法(ime),禁用后只能输入英文
  • 原文地址:https://www.cnblogs.com/alexyuyu/p/6247042.html
Copyright © 2020-2023  润新知