内置属性何时以及如何初始化?
__doc__
, __name__
(我想我知道这个:) ), __class__
,__setattr__
等等。
在我关于文档字符串的另一个问题中,其中一个答案提到文档字符串只是简单的字符串,我尝试使用'
,"
并且"""
它们都有效。但是,当我使用分配字符串值的变量并将该变量代替文档字符串时,它不起作用。这就是为什么我开始想知道__doc__
属性什么时候被初始化?
编辑:这是我在解释器上尝试过的(是的,这很疯狂,我很奇怪:D)
doc_str = "Says Hello world"
class HelloWorld():
def say():
doc_str
print("Hello world !")
h_w = HelloWorld()
h_w.say.__doc__
class AnotherHelloWorld():
def __init__(self, doc_str="Says HELLO WORLD"):
self.doc_str = doc_str
def say(self):
self.doc_str
print("HELLO WORLD !")
a_h_w = AnotherHelloWorld("Scream... HELLO WORLD!")
a_h_w.say.__doc__
class YetAnotherHelloWorld():
def __init__(self, doc_str="Still does't say HELLO WORLD :( "):
self.doc_str = doc_str
def say(self):
"%s"%self.doc_str
print("HELLO WORLD .. Again!")