看看这段代码:
class MyClass():
# Why does this give me "NameError: name 'self' is not defined":
mySelf = self
# But this does not?
def myFunction(self):
mySelf2 = self
Run Code Online (Sandbox Code Playgroud)
基本上我想要一个类来引用自己而不需要专门命名自己的方法,因此我希望自己为类工作,而不仅仅是方法/函数.我怎样才能做到这一点?
编辑:这一点是我试图从类内部引用类名称与self之类的东西.class ._ name _这样类名在代码的任何地方都没有硬编码,因此重用代码更容易.
编辑2:从我从下面的答案中学到的,我想要做的是不可能的.我必须找到一种不同的方式.任务放弃了.
编辑3:这是我正在尝试做的事情:
class simpleObject(object):
def __init__(self, request):
self.request = request
@view_defaults(renderer='string')
class Test(simpleObject):
# this line throws an error because of self
myClassName = self.__class__.__name__
@view_config(route_name=myClassName)
def activateTheView(self):
db = self.request.db
foo = 'bar'
return foo
Run Code Online (Sandbox Code Playgroud)
请注意,self在您希望类引用自身以使分配工作时,未定义此选项.这是因为(除了被任意命名),self指的是实例而不是类.在可疑的代码行试图运行的时候,还没有类可以引用它.不,它会引用类,如果有.
在方法中,您始终可以使用type(self).这将获得MyClass创建当前实例的子类.如果要进行硬编码MyClass,则该名称将在方法的全局范围内可用.这将允许您执行示例在实际工作时允许的所有内容.例如,你可以MyClass.some_attribute 在你的方法里面做.
您可能希望在创建类之后修改类属性.这可以通过装饰器或临时基础来完成.元类可能更适合.虽然不知道你真正想做什么,但这是不可能的.
这里有一些代码可以做你想要的.它使用元类AutoViewConfigMeta和新的装饰器来标记要view_config应用的方法.我欺骗了view_config装饰者.它会在调用时打印出类名,以证明它可以访问它.元类__new__只是遍历类字典并查找由auto_view_config装饰器标记的方法.它清除标记并使用适当的类名应用view_config装饰器.
这是代码.
# This just spoofs the view_config decorator.
def view_config(route=''):
def dec(f):
def wrapper(*args, **kwargs):
print "route={0}".format(route)
return f(*args, **kwargs)
return wrapper
return dec
# Apply this decorator to methods for which you want to call view_config with
# the class name. It will tag them. The metaclass will apply view_config once it
# has the class name.
def auto_view_config(f):
f.auto_view_config = True
return f
class AutoViewConfigMeta(type):
def __new__(mcls, name, bases, dict_):
#This is called during class creation. _dict is the namespace of the class and
# name is it's name. So the idea is to pull out the methods that need
# view_config applied to them and manually apply them with the class name.
# We'll recognize them because they will have the auto_view_config attribute
# set on them by the `auto_view_config` decorator. Then use type to create
# the class and return it.
for item in dict_:
if hasattr(dict_[item], 'auto_view_config'):
method = dict_[item]
del method.auto_view_config # Clean up after ourselves.
# The next line is the manual form of applying a decorator.
dict_[item] = view_config(route=name)(method)
# Call out to type to actually create the class with the modified dict.
return type.__new__(mcls, name, bases, dict_)
class simpleObject(object):
__metaclass__ = AutoViewConfigMeta
class Test(simpleObject):
@auto_view_config
def activateTheView(self):
foo = 'bar'
print foo
if __name__=='__main__':
t = Test()
t.activateTheView()
Run Code Online (Sandbox Code Playgroud)
如果您有任何疑问,请告诉我.
| 归档时间: |
|
| 查看次数: |
3536 次 |
| 最近记录: |