-3

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)
  • 5
    `class Foo(something)` isn't a function (it's a class definition) and `something` isn't a parameter (it's an inheritance list). I'm voting to close as a misunderstanding/unhelpful to future visitors; see [the docs](https://docs.python.org/3/tutorial/classes.html#inheritance). – ggorlen Sep 29 '20 at 19:29
  • I suggest you read more about classes and inheritence. There is no function call here. – Code-Apprentice Sep 29 '20 at 19:32
  • Coming from javascript? In Python, "class" is a core part of the language rather than syntatic sugar. – Paul Becotte Sep 29 '20 at 20:04
  • @PaulBecotte Yes I'am! Thank you took me a while to figure that out. –  Sep 29 '20 at 21:30

2 Answers2

2

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

Random Davis
  • 6,662
  • 4
  • 14
  • 24
0

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

Untrue
  • 26
  • 3