hen*_*tha 1 python variable-declaration
我之前在Google上搜索了一些与Python相关的问题,并偶然发现了这个页面.作者做了类似以下的事情:
class TestClass(object):
first = str()
def __init__(self):
self.first = "Hello"
Run Code Online (Sandbox Code Playgroud)
那个"声明"这个变量first
有什么意义呢?我以前从未见过这样做过,而且我不能为我的生活想到这样一种情况,即在为变量赋值之前创建一个变量是有益的.
上面的例子可能看起来像这样:
class TestClass(object):
def __init__(self, first="Hello"):
self.first = first
Run Code Online (Sandbox Code Playgroud)
......或者我错过了什么?
作者使用的事实
first = str()
Run Code Online (Sandbox Code Playgroud)
而不是
first = ''
Run Code Online (Sandbox Code Playgroud)
表演,一起设置self.first
在__init__
无论如何,有就是没有这样做,目的.
也许作者很困惑并且认为python变量需要首先声明 -_-(在查看链接时很明显)
归档时间: |
|
查看次数: |
238 次 |
最近记录: |