- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在搜索元类在 python 中的用法时发现了这个问题。这是一个很好的问题和一个很好的答案,See Here .但是当我按照这样的例子进行操作时:
class UpperAttrMetaclass(type):
def __new__(cls, name, bases, dct):
attrs = ((name, value) for name, value in dct.items() if not name.startswith('__'))
uppercase_attr = dict((name.upper(), value) for name, value in attrs)
return type.__new__(cls, name, bases, uppercase_attr)
class Foo(object):
__metaclass__=UpperAttrMetaclass
bar = 'bip'
然后:
print(hasattr(Foo,'bar'))
我希望它会输出False
,但结果却是True
似乎元类根本没有改变任何东西。我一定是弄错了。很高兴你能指出来!
编辑:我正在使用 IDLE 3.2,以使我的情况更清楚。
编辑:感谢所有的答案。我找到了一篇关于这个的好帖子。所以我把它贴在这里 Metaclass in python 2&3
最佳答案
你没有弄错。 print(hasattr(Foo,'bar'))
和 print dir(Foo)
给我:
False
['BAR', '__class__', ...
你的 Python 版本是多少?我的是 2.7.2
如果您使用 Python 3.x,请尝试删除 __metaclass__
属性并将您的类声明为
class Foo(object, metaclass=UpperAttrMetaclass):
...
关于Python Metaclass 使用时没有改变类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9254991/
尝试使用类创建 GUI,但我一直遇到此错误的问题。我不确定这意味着什么,因为据我所知我只有一节课,我的错误是: Traceback (most recent call last): File "C:/
我在书中遇到过以下 groovy 脚本代码。它给我带来了一些奇怪的输出。 class Person{ def work(){ println "work()" } def spor
如果我向类添加元方法,我希望它显示在 Class.metaClass.metaMethods 中。 .但情况似乎并非如此。特别是,如果我这样做: class Example { def rea
我想知道为什么使用两个不同的类,而不是只对两者都使用 Class 的原因。 最佳答案 简短的回答是“您认为类是系统范围类的实例是错误的,每个类实际上都是类特定元类的实例”,而且“它不会以任何其他方式工
以下代码是具有一个简单表的 SqlAlchemy ORM 的非常简单的实现。 Mytable 类试图从 BaseAbstract 继承。 代码抛出以下异常: Message: metaclass co
译注:这是一篇在Stack overflow上很热的帖子。提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解。他知道这肯定和自省有关,但仍然觉
我在搜索元类在 python 中的用法时发现了这个问题。这是一个很好的问题和一个很好的答案,See Here .但是当我按照这样的例子进行操作时: class UpperAttrMetaclass(t
我在Grails插件中使用了几种类别。例如。, class Foo { static foo(ClassA a,Object someArg) { ... } static bar(C
我遇到了以下陈述: (Metaclass class) new. "Uses the new of Behavior but throws error because Metaclass class
我正在寻找有关我的这项作业的一些说明。我们应该输入该图的代码(不是询问这里的任何人),但我不明白到底发生了什么。 根据我的研究,我知道所有类都是元类的实例,但我不明白的是对象框架、上下文和图表是否应该
假设我有一个类 class Foo: def __init__(self): pass def meth1(self, *args): do_stuff
我想编写一个程序,它接受一个数字 p 作为输入,并为一个遵循整数算术模 p 的数字生成一个类型构造函数作为输出。 目前为止 def IntegersModP(p): N = type('Inte
def metaclass; class << self; self; end; end 谁能帮我破译这条线。我猜它被挤成一个的事实也无济于事。但是我才 2 天前才开始研究 ruby,我担心我可能
我正在测试 groovy.sql.Sql 元类中的运行时更改,修改 createConnection 方法。我的目标是在请求某些连接时始终调用进程。 是否有更改 protected 方法的限制?我可以
Groovy 和其他 OO 编程语言中的 Meta-Class 有什么用? 最佳答案 您可能会想到 Groovy's MetaClass : A MetaClass within Groovy def
我有以下脚本: task myTask {} class Person { Person() { Person instance = this println
我使用的是 groovy 1.7.8。 我有以下代码: public class StaticClass { public static String getStaticString(Stri
在编写单元测试用例时,有一点我需要做一些元编程来测试如下方法。 void "test method:resolver"(){ setup:"mocked resolver"
我正在使用 django-tastypie 为我的 webapp 创建一个 rest API。我想创建如下所述的类,而不用显式地全部输入它们(我有超过 100 个类) class CityResour
例如,以下不会编译 (link to typescript playground example) : declare class ClassFactory { constructor();
我是一名优秀的程序员,十分优秀!