def index(self): [英] def index(self):
问题描述
< code>
class HelloWorld:
def index(self):
index.exposed = True
返回Hello world!
< / code>
和
< code>
class HelloWorld:
def index(self):
self.exposed = True
返回Hello world!
< / code>
Gert Cuykens kirjoitti:
是否有区别
< code> < br $>
类HelloWorld:
def index(self):
index.exposed = True
return" Hello world !"
< / code>
和
< code>
class HelloWorld:
def index(self):
self.exposed = True
return" Hello world!"
< / code>
常驻专家似乎缺席了一段时间,我会罢工:
是的:第一个给出了运行时错误,第二个是OK。
我已经将第二个类重命名为HelloWorld2然后:
< blockquote class =post_quotes>
>> hw = HelloWorld()
hw2 = HelloWorld2()
>> hw.index()
回溯(最近一次调用最后一次):
文件"< pyshell#144>",第1行,< module>
hw.index()
文件" C:\Python \Dive进入Python \Py \ ampihelper.py",第40行,索引
索引.exposed = True
NameError:全局名称''index''未定义
< blockquote class =post_quotes>
>> hw2.index()
''Hello world!''
错误消息显示Python编译器解释了
构造' 'index.exposed''指的是一个全局变量''index'',它在运行时不存在
。第二类成功定义了一个
实例属性''公开'',如下所示:
>> print hw2.exposed
True
HTH
Jussi
Gert Cuykens a ??暴击:
是否有区别
< code>
class HelloWorld:
def index(self):
index.exposed = True
返回Hello world!
< / code>
和
< code>
class HelloWorld:
def index(self):
self.exposed = True
返回Hello world!
< / code>
问问自己这个函数的
上下文中有什么名称''index'和''self'',你会得到答案。如果你不能回答,那么可能是学习Python的时候了......
FWIW,第一个版本引发异常(除非当然,名称
''index''已经包含在封闭范围内)。并且第二个不会像CherryPy那样工作。
FWIW,第一个版本引发异常(除非名称
''index''已绑定在封闭范围内)。并且第二个不可能
可能与CherryPy一样正常工作。
< code>
class HelloWorld:
def index(self):
返回Hello world!
index.exposed = True #DOOOOOOH!
< / code>
i跳过阅读关于2.1.8缩进的章节。猜猜实现两个空间需要多少时间才能实现2个空间不等于1个空间大声笑了
任何其他章节我应该读水平而不是垂直for a
php5 htm jso wane是蛇用户:)
Is there a difference between
<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>
and
<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>
Gert Cuykens kirjoitti:Is there a difference between
<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>
and
<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>The resident experts seemingly being absent for a while, I''ll strike:
Yes: the first gives a runtime error and the second is OK.
I''ve renamed the second class to HelloWorld2 and then:
>>hw = HelloWorld()
hw2 = HelloWorld2()
>>hw.index()
Traceback (most recent call last):
File "<pyshell#144>", line 1, in <module>
hw.index()
File "C:\Python\Dive into Python\Py\apihelper.py", line 40, in index
index.exposed = True
NameError: global name ''index'' is not defined
>>hw2.index()
''Hello world!''
The error message shows that the Python compiler has interpreted the
construction ''index.exposed'' to refer to a global variable ''index'' that
doesn''t exist at run time. The second class succesfully defines an
instance attribute ''exposed'' as can be seen by:
>>print hw2.exposed
True
HTH
Jussi
Gert Cuykens a ??crit :Is there a difference between
<code>
class HelloWorld:
def index(self):
index.exposed = True
return "Hello world!"
</code>
and
<code>
class HelloWorld:
def index(self):
self.exposed = True
return "Hello world!"
</code>Ask yourself what are the names ''index'' and ''self'' refering to in the
context of this function, and you''ll have the answer. And if you can''t
answer, then it may be time to learn Python...
FWIW, the first version raises an exception (unless of course the name
''index'' is already bound in the enclosing scope). And the second won''t
probably work as expected with CherryPy.
FWIW, the first version raises an exception (unless of course the name''index'' is already bound in the enclosing scope). And the second won''t
probably work as expected with CherryPy.<code>
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True #DOOOOOOH!
</code>
i skipped reading the chapter about 2.1.8 Indentation. Guess how many
hours it took to realize 2 spaces isn''t the same as 1 space lol
Any other chapters i should read horizontal instead of vertical for a
php5 htm jso wane be snake user :)
这篇关于def index(self):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!