1

In variable y I want to call only the second value i.e age while keeping the first value (height) as the default. How do I do that?

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))
Heikki
  • 2,214
  • 19
  • 34
  • Please consider accepting the answer the most helpful and the most complete ;) for the next ones that’ll comme here – azro Oct 18 '20 at 10:52

2 Answers2

2

You may use keyword argument : use its name and its position in the method

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)

Here are some more

azro
  • 53,056
  • 7
  • 34
  • 70
0

Use a keyword argument:

y = person(age=32)

Kamal Sharma
  • 70
  • 1
  • 6