python调用函数出现未定义_python – 为什么函数参数之外的“self”会给出“未定义”的错误?...

看看这段代码:

class MyClass():

# Why does this give me "NameError: name 'self' is not defined":

mySelf = self

# But this does not?

def myFunction(self):

mySelf2 = self

基本上我想要一个类来引用自己而不需要专门命名自己的方法,因此我希望自己为类工作,而不仅仅是方法/函数.我怎样才能做到这一点?

编辑:这一点是我试图从类内部引用类名称,类似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

最佳答案 请注意,当您希望类引用自身以使赋值工作时,不会定义self.这是因为(除了被任意命名),self指的是实例而不是类.在可疑的代码行试图运行的时候,还没有类可以引用它.如果有的话,它并不是指代课程.

在方法中,您始终可以使用类型(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()

如果您有任何疑问,请告诉我.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值