如何仅调用 python 中 class 的第二个值,同时将第一个值保持为默认值?
How to call only the second value of a class in python, while keeping the first value as default?
在变量 y
中,我只想调用第二个值,即 age
,同时将第一个值 (height
) 保留为默认值。我该怎么做?
class person():
def __init__(self, height=130, age=20):
self.height = height
self.age = age
def convert_height(self):
return self.height / 10
def find_birth_year(self, p_year):
return p_year - self.age
x = person(170)
y = person(, 32) # How to have default value for height here?
print(x.height, x.age)
print(y.height, y.age)
print(x.convert_height(), x.find_birth_year(2020))
print(y.convert_height(), y.find_birth_year(2020))
您可以使用 keyword
参数:使用其名称及其在方法中的位置
class person():
def __init__(self, height=130, age=20):
self.height = height
self.age = age
if __name__ == '__main__':
x = person(170)
y = person(age=32)
这里是some more
使用关键字参数:
y = person(age=32)
在变量 y
中,我只想调用第二个值,即 age
,同时将第一个值 (height
) 保留为默认值。我该怎么做?
class person():
def __init__(self, height=130, age=20):
self.height = height
self.age = age
def convert_height(self):
return self.height / 10
def find_birth_year(self, p_year):
return p_year - self.age
x = person(170)
y = person(, 32) # How to have default value for height here?
print(x.height, x.age)
print(y.height, y.age)
print(x.convert_height(), x.find_birth_year(2020))
print(y.convert_height(), y.find_birth_year(2020))
您可以使用 keyword
参数:使用其名称及其在方法中的位置
class person():
def __init__(self, height=130, age=20):
self.height = height
self.age = age
if __name__ == '__main__':
x = person(170)
y = person(age=32)
这里是some more
使用关键字参数:
y = person(age=32)