Python类练习错误提示NameError: name 'self' is not defined,pythonnameerror,class Animal
Python类练习错误提示NameError: name 'self' is not defined,pythonnameerror,class Animal
class Animal: def __init__(self, animal): self.animal = animal def type(self, type=self.animal): print type
运行的时候出现 NameError: name 'self' is not defined?
方法参数的默认值是在函数定义的时候初始化的,而self指该class的实例化类,只有实例化之后才有值,因此这里编译出错(不是运行时错误)
如果打印的默认值非要设为self.animal的话,试试这样:
class Animal(object): def __init__(self,animal): self.animal = animal def type(self,type=None): print type if type else self.animal
你还需要了解一下self,在类中哪里可以访问得到self,哪里不可以!
编橙之家文章,
相关内容
- uwsgi: invalid option是什么错误?,uwsgioption,比如下面这个命
- Python缺省参数应该怎么理解它的作用,python缺省参数理
- Python3.3代码代码数据引用问题求解,python3.3求解,如题,
- swoole可不可以替代ajax轮询,swooleajax轮询,swoole可以代替
- Python读取数据库后如何生成html网页,python数据库,数据
- 请问Numpy里有可以实用且高效制矩阵的好方法吗?,num
- django1.6 shell下访问对象出错AttributeError,,开发环境是
- Flask响应内容为图片时怎么体现,,比如请求 http://
- 想用Python去掉{通配符}正则表达式怎么写,python正则表
- Python音频格式转换问题m4a转amr,pythonm4a,如何实现m4a 转
评论关闭