Python parameter behavior

70 Views Asked by At

My understanding of function parameters in Python is that they are like empty objects until function call and that's it. eg.

def square(x): #where x is an empty var defined here.
    return x*x #for the scope of this function

So how does this work, where "NamedTuple" is a callback function I assume? Does "NamedTuple" return value gets used as the parameter to class Car()?

>>> from typing import NamedTuple

>>> class Car(NamedTuple):
...     color: str
...     mileage: float
...     automatic: bool

>>> car1 = Car("red", 3812.4, True)

>>> # Instances have a nice repr:
>>> car1
Car(color='red', mileage=3812.4, automatic=True)
2

There are 2 best solutions below

0
On

class Car(NamedTuple): is a class declaration, not a function call. NamedTuple is a class that Car is derived from.

0
On

No, NamedTuple is not a function, but a class. This is called inheritance (or subclassing), more about it here.