看看这段代码:

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()

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

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

  1. split函数python 未定义_python split函数基本用法

    Python split()方法描述: Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分割成 (num+1) 个子字符串.语法: str.split(st ...

  2. python输入python显示未定义_python 未定义

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! sdk 3.0 实现了统一化,各个语言版本的 sdk具备使用方法相同.接口调用方 ...

  3. html 超链接 javascript 函数 java 未定义_JavaScript 学习笔记(一)

    本系列适合作为JS的复习文档. 学习JavaScript,不要以为会做一两个如图片切换.tabs选项卡这样特效,就是精通JavaScript了.JavaScript不仅仅是用来做一两个特效,它更大的用 ...

  4. matlab 中数据未定义,matlab总是出现参数未定义

    matlab 曲线拟合参数 1.你给的数据有问题,t有11个点,而R只有10个点.暂按照删去t的最后一个点考虑.2.拟合和很多因素有关,比如初值.拟合函数的选择.算法设置等.3.按现有程序拟合的结果: ...

  5. c语言不定长参数函数,DEFUN 如何定义支持不定长参数的函数

    当前位置:我的异常网» C语言 » DEFUN 如何定义支持不定长参数的函数 DEFUN 如何定义支持不定长参数的函数 www.myexceptions.net  网友分享于:2015-07-15   ...

  6. MDK:assert_param函数未定义的错误:Error: L6218E

    今天使用奋斗stm32开发板,编译程序时 出现了一下错误,网上有很多解决方案,可是一直没解决, 在链接过程中出现assert_param函数未定义的错误:Error: L6218E: Undefine ...

  7. 笔记-- vs2008 ,右键转到定义,弹出“未定义符号”

    具体情况为:新建一个项目,将原来某工程的.cpp和.h文件拷贝过来,还有一个工程,都加入该项目:编译,通过:但右键转到定义,弹出"未定义符号".与这一现象同时出现的,是类视图里并没 ...

  8. python调用js文件报错_python - selenium 运行网页中js脚本报错,提示未定义

    问 题 问题1 selenium 运行网易中js脚本报错提示未定义 报错提示如下: driver.execute_script("javascript:amsInit(62800,30315 ...

  9. python函数打上去说未定义_Python:名称未定义错误,即使函数被明确定义为b

    我是python新手,在函数定义方面有一个奇怪的问题.我已经检查了论坛,并确保在调用之前定义了我的函数,但是这并没有帮助解决问题.当我试图在这个特定的方法中按字面方式调用函数时,总是会遇到一个未定义名 ...

最新文章

  1. 我所了解的Linux运维技术
  2. Mysql性能优化实践
  3. numpy中amin()方法中维度axis=0 1 2 的理解
  4. vbox 设置linux共享文件夹,Windows 7下设置VirtualBox中Linux共享文件夹
  5. Orace 11g 监听 配置修改 说明
  6. 快速找到message toast弹出的application代码位置
  7. 移动端取消iphone ipad默认按钮
  8. Apache重定向方法实现图片防盗链
  9. 数组 -- 13.2 Maximum Subarray --图解
  10. android imagebutton 动画,android – ImageButton Icon Tint基于State
  11. 常用BUG管理工具系统
  12. IDEA:Cannot determine Java VM executable in selected JDK
  13. 台式机怎么开启 无线lan服务器,台式机没有无线网卡怎么样实现上网
  14. 百度自动php推送蜘蛛怎么不来访问,使用代码向百度蜘蛛主动推送链接
  15. C++基础知识(7)
  16. python判断是否闰年_【python】判断年份是否为闰年
  17. 【文献心得】内存隔离技术
  18. 【温州死中】温州RAP
  19. 卧槽,这也真的太上头了吧
  20. portal服务器认证系统有哪些,portal服务器认证过程问题

热门文章

  1. Spring Security 实战干货:从零手写一个验证码登录
  2. FastJson屡爆漏洞,或许你应该试试它?
  3. 如何通过抓包实战来学习Web协议?
  4. 帝国cms后台上传大视频上传m3u8批量上传一次多个的解决思路和方法
  5. python 数组数据类型
  6. github搜索不能用
  7. math range error
  8. 机器学习:多分类的logistic回归
  9. php 请求拦截,解决拦截器对ajax请求的拦截实例详解
  10. evcdf matlab,求助大神