Pau*_*nta 4 c++ python metaprogramming class introspection
当我导入一个具有类的模块时,在首次读取该类并创建类对象时执行什么代码?有什么方法可以影响发生的事情吗?
编辑:我意识到我的问题可能有点过于笼统......
我正在寻找更低级别的东西,这将允许我从C++进行内省.我用Python扩展我的C++应用程序.我有一些在C++中定义并在Python中公开的类.用户可以在脚本中继承这些类,并且我希望能够在首次定义时获取有关它们的详细信息.
许多可能的事情都会发生.最基本的:
class首次读取时执行块的内容.
要查看此操作,此示例:
class Foo(object):
print "bar"
def __init__(self):
print "baz"
Run Code Online (Sandbox Code Playgroud)
将bar在导入模块时打印.
如果类定义了元类,则在__new__运行类代码块之后将运行元类函数.
例:
class MyMeta(type):
def __new__(mcs, name, bases, kwargs):
print "I'm the metaclass, just checking in."
return type.__new__(mcs, name, bases, kwargs)
class Foo(object):
__metaclass__ = MyMeta
print "I'm the Foo class"
Run Code Online (Sandbox Code Playgroud)
输出:
I'm the Foo class
I'm the metaclass, just checking in.
Run Code Online (Sandbox Code Playgroud)
我确信其他位也可以运行,这些正是我所熟悉的.
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |