从全球范围内的Metaclasses提供课程 [英] Making classes from Metaclasses globally available

查看:60
本文介绍了从全球范围内的Metaclasses提供课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用MetaClasses创建课程。


如何全局创建这些新课程?可用吗?


我可能只需要将它们分配给一些神奇的东西,但我似乎无法确定是否需要b $ b来确定哪一个。


如果我这样做:


MetaClass(''Klass'',(),{})


结果类需要分配给某些东西:


myclass = MetaClass(''Klass'',(),{})


问题是我正在循环而且事先并不知道会有多少课程。


所以现在我有一些东西喜欢:


msclasses [clsname] = MapServerMetaClass(s​​tr(clsname),(),

{''schema'':list,''__ init__' ':MapServerClassInit})

循环中的



问题是我真的不想让这些课程陷入其中。

我想要他们全球好像我只是在文件顶部做了一个类

声明。


任何想法或建议?


谢谢,

JF

解决方案

Jean-Fran?ois Doyon写道:

我正在使用MetaClasses来创建类。

如何在全局范围内创建这些新类?可用?




据我所知,你的问题与元类无关。

globals()[" foo"] = 1
foo



1


用新班级替换上面的1,你就是。注意,

但是,动态插入变量并不是最好的编程实践。如果您事先不知道变量名称,那么能够通过其标识符访问对象的好处是什么?


Peter


Jean-Fran?ois Doyon写道:

你好,

我我正在使用MetaClasses来创建类。

如何在全局范围内创建这些新类?可用吗?

我可能只需要将它们分配给一些神奇的东西,但我似乎无法找出哪一个。

如果我这样做:

MetaClass(''Klass'',(),{})

生成的类需要分配给某些东西:

myclass = MetaClass(''Klass'',(),{})




只做


类Klass(对象):

__metaclass__ = MetaClass


干杯


Stephan


< blockquote> Peter Otten< __ ******* @ web.de>写道:

但是,请注意,动态插入变量并不是最好的编程实践。当您不知道
advance中的变量名称时,能够通过其标识符访问
对象会有什么好处?




当_who_在_what_之前不知道变量名时?

dir()
[< stuff> ]来自foo import *
dir(foo)
[< stuff>,''hello''] hello()
''你好,世界!''


这里我们动态地将预先未知的变量插入

用户,即使事先已知模块编写者。


Not确信?


OK ...
来自treebuilder的
导入build_structure_from_xml_tree
dir()
[< stuff> ] build_structure_from_xml_tree(''fubar.xml'')
dir()
[< stuff>,''root''] dir(root)



[''branch1'',''branch2'']

这里我们动态插入事先未知的变量给

作者模块,

用户可能知道也可能不知道。如果用户是fubar.xml的作者,那么大概他可以知道变量名称;如果他不是,那么他可能不会事先知道他们。无论哪种方式,他都可能想要通过

名称来访问它们。


....仍然不相信?


我有一个解析C ++头文件的程序,并为其中的内容创建Python

代理,允许您与C ++库中的
对象进行交互。你加载了我们称之为字典的东西

(描述了这个库),程序使用这些信息来支持
动态地创建具有相同名称和结构的Python对象。 />
作为库中的C ++类。


程序的作者当然不知道

对象的名称程序将在未来创造。客户可能会知道他们(他可能想要用Python重写一些C ++代码),或者不需要
(他可能想要使用Python的内省功能) to

捅C ++库。)


Hello,

I''m using MetaClasses to create classes.

How do I make these new classes "globally" available?

I probably just have to assign them to something magic, but I can''t seem to
figure out which one.

if I do:

MetaClass(''Klass'', (), {})

The resulting class needs to be assigned to something:

myclass = MetaClass(''Klass'', (), {})

The problem is that I''m looping and don''t know in advance how many classes
there will be.

So right now I have something like:

msclasses[clsname] = MapServerMetaClass(str(clsname), (),
{''schema'':list,''__init__'':MapServerClassInit})

inside a loop.

Problem is I really don''t want to have the classes stuck inside that dict.
I want them "globally" available as if I had simply made a "class"
declaration at the top of the file.

Any ideas or suggestions?

Thanks,
J.F.

解决方案

Jean-Fran?ois Doyon wrote:

I''m using MetaClasses to create classes.

How do I make these new classes "globally" available?



As far as I can see, your problem has nothing to do with metaclasses.

globals()["foo"] = 1
foo


1

Substitute 1 in the above with your new class, and there you are. Note,
however, that dynamically inserting variables is not the best programming
practice. When you don''t know the variable name in advance, what would be
the benefit of being able to access the object via its identifier?

Peter


Jean-Fran?ois Doyon wrote:

Hello,

I''m using MetaClasses to create classes.

How do I make these new classes "globally" available?

I probably just have to assign them to something magic, but I can''t seem
to figure out which one.

if I do:

MetaClass(''Klass'', (), {})

The resulting class needs to be assigned to something:

myclass = MetaClass(''Klass'', (), {})



Just do

class Klass(object):
__metaclass__ = MetaClass

Cheers

Stephan


Peter Otten <__*******@web.de> writes:

Note, however, that dynamically inserting variables is not the best
programming practice. When you don''t know the variable name in
advance, what would be the benefit of being able to access the
object via its identifier?



When _who_ doesn''t know the variable name in advance of _what_ ?

dir() [ <stuff> ] from foo import *
dir(foo) [ <stuff>, ''hello''] hello() ''Hello, World!''

Here we dynamically inserted variables not known beforehand to the
user, even if they were known beforehand to the module writer.

Not convinced?

OK ...
from treebuilder import build_structure_from_xml_tree
dir() [ <stuff> ] build_structure_from_xml_tree(''fubar.xml'')
dir() [ <stuff>, ''root'' ] dir(root)


[''branch1'', ''branch2'']
Here we dynamically inserted variables not known beforehand to the
author of the module, which may or may not have been known to the
user. If the user were the author of fubar.xml then, presumably he
would know the variable names; if he were not, then he may well not
know them beforehand. Either way he is likely to want to access them by
name.

.... still not convinced ?

I have a program which parses C++ header files and creates Python
proxies for the stuff found therein, allowing you to interact with
objects in C++ libraries. You load something we call a dictionary
(which describes the library) and the program uses that information to
create, dynamically, Python objects with the same names and structures
as the C++ classes in the library.

The author of the program certainly does not know the names of the
objects the program is going to create in the future. The client may
know them (he might be wanting to rewrite some C++ code in Python), or
not (he might be wanting to use Python''s introspection capabilities to
poke around the C++ library).


这篇关于从全球范围内的Metaclasses提供课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆