Python中文
首页
教程
问答
标签
搜索
登录
注册
Python TypeError:“str”对象不能为类调用
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>请帮助我理解这一点。我创建了一个非常简单的程序来尝试理解类。</p> <pre><code>class One(object): def __init__(self, class2): self.name = 'Amy' self.age = 21 self.class2 = class2 def greeting(self): self.name = raw_input("What is your name?: ") print 'hi %s' % self.name def birthday(self): self.age = int(raw_input("What is your age?: ")) print self.age def buy(self): print 'You buy ', self.class2.name class Two(object): def __init__(self): self.name = 'Polly' self.gender = 'female' def name(self): self.gender = raw_input("Is she male or female? ") if self.gender == 'male'.lower(): self.gender = 'male' else: self.gender = 'female' self.name = raw_input("What do you want to name her? ") print "Her gender is %s and her name is %s" % (self.gender, self.name) Polly = Two() Amy = One(Polly) # I want it to print Amy.greeting() Amy.buy() Amy.birthday() </code></pre> <p>问题代码</p> <pre><code>Polly.name() # TypeError: 'str' object is not callable Two.name(Polly)# Works. Why? </code></pre> <p>为什么对类实例Polly调用方法不起作用?我迷路了。我已经看了<a href="http://mail.python.org/pipermail/tutor/2003-May/022128.html">http://mail.python.org/pipermail/tutor/2003-May/022128.html</a>和其他类似的Stackoverflow问题,但我没有理解。非常感谢。</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您正在用名为<code>name</code>的方法覆盖您的<code>name</code>属性。只是重命名一些东西。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
3 回答
如何合并多个xarray数据变量及其坐标?
4 回答
如何合并多个列中具有重复值的行
1 回答
如何合并多个唯一id
7 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
5 回答
如何合并多个字典并添加同一个键的值?(Python)
5 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
9 回答
如何合并多个数据帧
4 回答
如何合并多个数据帧并使用Pandas为假人添加列?
10 回答
如何合并多个数据帧并按时间戳排序
3 回答
如何合并多个数据帧的列表并用另一个lis标记每列
2 回答
如何合并多个数据框中的列
10 回答
如何合并多个文件?
5 回答
如何合并多个查询集?
1 回答
如何合并多个绘图?
10 回答
如何合并多个词典
5 回答
如何合并多个输入数据集(数据帧)?
2 回答
如何合并多条记录中拆分的文本行
5 回答
如何合并多索引列datafram
3 回答
如何合并多级(即多索引)数据帧?
5 回答