__init__括号里的东西叫什么?
What are the things in the __init__ parentheses called?
调用的init括号里的东西是什么
class Mother:
def __init__ (self, strName = "Maya", strAddress = "717 Lynx Street"):
self.name = strName
self.address = strAddress
def play(self):
print("Playing games.")
def __str__ (self):
return self.name + " at " + self.address + " "
class Son(Mother):
pass
mySon = Son()
mySon.name = "Jeff"
mySon.play()
print(mySon)
什么是 strName = "Maya" 和 strAddress = "717 Lynx Street"?他们叫什么?谢谢。
函数参数(或仅 "arguments"),在本例中为具有默认值的参数,使它们成为可选参数。
它们是方法 __init__
的参数。如果您在参数名称后使用 =
指定一个值,它将成为该参数的默认值。
一些例子
>>> m1 = Mother("Juliana", "123 Apple Street")
>>> m2 = Mother("Francisca")
>>> m3 = Mother()
>>> m1.strName
'Jualiana'
>>> m1.strAddress
'123 Apple Street'
>>> m2.strName
'Francisca'
>>> m2.strAddress
'717 Lynx Street'
>>> m3.strName
'Maya'
>>> m3.strAddress
'717 Lynx Street'
您正在将 class 继承应用于您的 class Son
。这意味着它将从您的基础 class Mother
继承所有实例变量和方法。因此,您继承了使用这些默认值的 __init__
方法。
调用的init括号里的东西是什么
class Mother:
def __init__ (self, strName = "Maya", strAddress = "717 Lynx Street"):
self.name = strName
self.address = strAddress
def play(self):
print("Playing games.")
def __str__ (self):
return self.name + " at " + self.address + " "
class Son(Mother):
pass
mySon = Son()
mySon.name = "Jeff"
mySon.play()
print(mySon)
什么是 strName = "Maya" 和 strAddress = "717 Lynx Street"?他们叫什么?谢谢。
函数参数(或仅 "arguments"),在本例中为具有默认值的参数,使它们成为可选参数。
它们是方法 __init__
的参数。如果您在参数名称后使用 =
指定一个值,它将成为该参数的默认值。
一些例子
>>> m1 = Mother("Juliana", "123 Apple Street")
>>> m2 = Mother("Francisca")
>>> m3 = Mother()
>>> m1.strName
'Jualiana'
>>> m1.strAddress
'123 Apple Street'
>>> m2.strName
'Francisca'
>>> m2.strAddress
'717 Lynx Street'
>>> m3.strName
'Maya'
>>> m3.strAddress
'717 Lynx Street'
您正在将 class 继承应用于您的 class Son
。这意味着它将从您的基础 class Mother
继承所有实例变量和方法。因此,您继承了使用这些默认值的 __init__
方法。