最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Default of an argument in two different spots - Stack Overflow

programmeradmin5浏览0评论

I'm doing exercises on pynative. The url is:

I'm stuck on OOP Exercise 4: Class Inheritance.

Here are the given instructions: Given:

Create a Bus class that inherits from the Vehicle class. Give the capacity argument of Bus.seating_capacity() a default value of 50.

Use the following code for your parent Vehicle class.

Here's the given code:

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"

Here is the expected output:

The seating capacity of a bus is 50 passengers

Here's the solution that was provided on the website:

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"

class Bus(Vehicle):
    # assign default value to capacity
    def seating_capacity(self, capacity=50):
        return super().seating_capacity(capacity=50)

School_bus = Bus("School Volvo", 180, 12)
print(School_bus.seating_capacity())

What I dont get is why the default for capacity was declared twice. Also I'm not getting why super() was declared.

I tried to remove the default declaration from capacity from both lines (I played around with it a bit), but I kept getting errors.

I'm doing exercises on pynative. The url is: https://pynative/python-object-oriented-programming-oop-exercise/#h-oop-exercise-4-class-inheritance

I'm stuck on OOP Exercise 4: Class Inheritance.

Here are the given instructions: Given:

Create a Bus class that inherits from the Vehicle class. Give the capacity argument of Bus.seating_capacity() a default value of 50.

Use the following code for your parent Vehicle class.

Here's the given code:

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"

Here is the expected output:

The seating capacity of a bus is 50 passengers

Here's the solution that was provided on the website:

class Vehicle:
    def __init__(self, name, max_speed, mileage):
        self.name = name
        self.max_speed = max_speed
        self.mileage = mileage

    def seating_capacity(self, capacity):
        return f"The seating capacity of a {self.name} is {capacity} passengers"

class Bus(Vehicle):
    # assign default value to capacity
    def seating_capacity(self, capacity=50):
        return super().seating_capacity(capacity=50)

School_bus = Bus("School Volvo", 180, 12)
print(School_bus.seating_capacity())

What I dont get is why the default for capacity was declared twice. Also I'm not getting why super() was declared.

I tried to remove the default declaration from capacity from both lines (I played around with it a bit), but I kept getting errors.

Share Improve this question edited 2 days ago deceze 523k88 gold badges800 silver badges943 bronze badges asked 2 days ago Anthony BennettAnthony Bennett 31 bronze badge New contributor Anthony Bennett is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 4
def seating_capacity(self, capacity=50):
    return super().seating_capacity(capacity=50)

Yeah, this is nonsense. The first capacity=50 declares a parameter with the default value 50. The second capacity=50 passes the value 50 to the named parameter capacity for the call to super().seating_capacity. Either of those by itself is fine, but both together make no sense here, since the first parameter-with-default-value isn't being used for anything. Correctly it should be:

def seating_capacity(self, capacity=50):
    return super().seating_capacity(capacity)

This is overriding the parent's seating_capacity method, providing a default value for the capacity parameter, then calls the parent's seating_capacity implementation, passing it the capacity value (which may be the default 50 or whatever the caller passed to School_bus.seating_capacity(..)).

Even this is debatable from a SOLID perspective, as it changes the function signature of seating_capacity from requiring an argument to not requiring one; but at least that doesn't break anything per se.

发布评论

评论列表(0)

  1. 暂无评论