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‘)

时间: 2024-09-29 02:21:22

python class metaclass instance的相关文章

python: class VS instance

在CPP中我们认为class是一个对象的定义,instance就是这个对象的具体实例,所以class没有任何现实的意义不能提供直接的操作.但是在python里面就是完全不一样的世界,python秉承了部分java的every is object的设计理念,那对class本身当然也不能放过,所以这里的class也同样是一个实例,只不过只此一份,不能随便派生,自然我们联想到了CPP中的singlton, 在很大程度来讲两者的作用是相同的,但是从设计理念上来讲两者又是完全不同的,当然在python中也

[python] 理解metaclass并实现一个简单ORM框架

metaclass 除了使用type()动态创建类以外,要控制类的创建行为,还可以使用metaclass. metaclass,直译为元类,简单的解释就是: 当我们定义了类以后,就可以根据这个类创建出实例,所以:先定义类,然后创建实例. 但是如果我们想创建出类呢?那就必须根据metaclass创建出类,所以:先定义metaclass,然后创建类. 连接起来就是:先定义metaclass,就可以创建类,最后创建实例. 所以,metaclass允许你创建类或者修改类.换句话说,你可以把类看成是met

python 的 metaclass

Python 是一种非常灵活的语言,这使得它具有很强的表达力,metaclass 也是这种强大表达力的一个体现. 啥是 metaclass 呢? meta- 这个词似乎这些年越来越流行,翻译成中文,一般是  元..., 比如元认知,元数据.“元什么” 一般表示的含义是 什么的什么, 元数据即数据的数据,元认知即认知的认知.metaclass 就是类的类.粗略的说,执行一个 metaclass 会得到一个新的 class, Python 内置的 type 就可以看成一个metaclass. met

Python Simple Unicode Instance

#!/usr/bin/env python #--*-- coding:utf-8 --*-- ''' An example of reading and writing Unicode string :Writes a Unicode string to a file in utf-8 and reads it back in. ''' CODEC = 'utf-8' FILE = 'unicode.txt' hello_out = u"Hello world\n" bytes_ou

Python里面所有instance应该现在__init__里面预定义

原因: Yes, you should assign all attributes of your object in the __init__ method. The most important reason to do this is tool support. Many Python IDEs can see the assignments in the __init__ and use those attributes for type-based autocompletion. Sp

python type metaclass

obj=Foo()##Foo类是MyType类的对象 所以先执行MyType的init  Foo()加括号执行MyType的call foo=type('Foo',(object,),{'say':lambda a:123}) ##所有类都是type类的对象 print(foo().say())

Python type class metaclass

'type' 是 python built-in metaclass 其他继承自 ‘type’的class都可以是 Metaclass 子类可以继承父类的metaclass 然而 __metaclass__属性不能继承 __metaclass__可以定义成任何返回instance of 'type' callable __new__(cls, ...)  : create instance of cls __init__(self, ...) :instance already created

深刻理解Python中的元类(metaclass)

译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得不太明白,希望大家可以给出一些实际的例子和代码片段以帮助理解,以及在什么情况下需要进行元编程.于是e-satis同学给出了神一般的回复,该回复获得了985点的赞同点数,更有人评论说这段回复应该加入到Python的官方文档中去.而e-satis同学本人在Stack Overflow中的声望积分也高达6

[转] 深刻理解Python中的元类(metaclass)

非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stackoverflow.欢迎加入翻译组. 译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得不太明白,希望大家可以给出一些实际的例子和代码片段以帮助理解,以及