My situation:
Below the documentation of the a module in the manim library (found here) there have been some __init__
arguments which confused me and I have not been able to reproduce them with a testing class.
A code snippet from the library:
def __init__(
self,
x_range: Sequence[float] | None = None, # must be first
length: float | None = None,
...
line_to_number_buff: float = MED_SMALL_BUFF,
decimal_number_config: dict | None = None,
numbers_to_exclude: Iterable[float] | None = None,
**kwargs,
):
...
Question: Now I am wondering about multiple statements there. How can I understand the following code elements:
x_range: Sequence[float] | None = None
length: float | None = None
line_to_number_buff: float = MED_SMALL_BUFF
decimal_number_config: dict | None = None
numbers_to_exclude: Iterable[float] | None = None
Precise questions:
- What happens if e.g. x_range is no
Sequence
and what happens if theSequence
is not of typefloat
? - What happens for point 3 above if no entry of type
float
if passed into the argument? Is theMED_SMALL_BUFF
a value if no specific value is specified?
My assumptions:
- The colons specify the type of the element which are passed into the
__init__
dunder-method for the arguments. - The
|
is an logical or which sets the value before the colos equal toNone
. Nontheless I don't understand whichNone
is set toNone
byNone = None
.
CodePudding user response:
The values after =
are the default values for the parameters. For instance, if you don't pass the line_to_number_buff
argument, it defaults to MED_SMALL_BUF
.
Where it has sometype | None
, it means you can pass either a value of sometype
or you can pass None
. You're not generally expected to pass None
explicitly (but you can); these parameters default to None
, and the type hint allows for this.
If you don't pass a value of the type specified, you'll probably get an error. The function might check the type explicitly and report a TypeError
itself, but more likely it just uses the value when calling some other code that expects that type. The bad value could propagate through numerous functions before it gets to something that actually checks, and that will report the error.