Python 3:为动态类设置名称空间

时间:2018-10-10 21:04:18

标签: python python-3.x class metaprogramming

我正在编写一个Python(3.6)程序,以从用户提供的Yaml文件动态生成类。

使用type()的过程看起来非常简单,但是我无法控制所生成类的模块名称。

一个最小的例子:

>>> x = type('hey.Ho', (), {})
>>> x
__main__.hey.Ho

此外(更接近我的真实世界的代码):

>>> from abc import ABCMeta
>>> class BaseClass(metaclass=ABCMeta):
...    pass

>>> x = type('hey.Ho', (BaseClass,), {})
>>> x
abc.hey.Ho

如何获取新的类命名空间为hey.Ho

1 个答案:

答案 0 :(得分:2)

您要设置类的Using module file /Library/Python/2.7/site-packages/ansible/modules/windows/win_scheduled_task.ps1 <xx.xx.xxx.x> ESTABLISH WINRM CONNECTION FOR USER: ak on PORT 5986 TO xx.xx.xxx.x checking if winrm_host xx.xx.xxx.x is an IPv6 address <xx.xx.xxx.x> WINRM CONNECT: transport=basic endpoint=https://xx.xx.xxx.x:5986/wsman <xx.xx.xxx.x> WINRM OPEN SHELL: 0D4636FD-D80A-42AD-AB4B-D1F4DFD24EC1 EXEC (via pipeline wrapper) <xx.xx.xxx.x> WINRM EXEC 'PowerShell' ['-NoProfile', '**-NonInteractive**', '-ExecutionPolicy', 'Unrestricted', '-'] <xx.xx.xxx.x> WINRM RESULT u'<Response code 0, out "{"changed":false}\r\n", err "">' <xx.xx.xxx.x> WINRM STDOUT {"changed":false} <xx.xx.xxx.x> WINRM STDERR <xx.xx.xxx.x> WINRM CLOSE SHELL: 0D4636FD-D80A-42AD-AB4B-D1F4DFD24EC1 ok: [xx.xx.xxx.x] => { "changed": false } META: ran handlers META: ran handlers 属性:

__module__