On 2020-05-06, at 13:20, Louis St-Amour
wrote: In TypeScript "optional" works fine for naming the addition of null/etc. -- it isn't an issue because for JS, all arguments are optional if "undefined" is an allowed value and so TS requires all method arguments when "undefined" is not allowed. In TS, ? is shorthand for adding both null and undefined and allowing undefined is thus what makes it optional, no need to explicitly define a default value. This is described here: https://www.typescriptlang.org/docs/handbook/functions.html#optional-and-def...
In Python, the other way we could work around this is if Optional[int] were given the additional behavior of assigning the default value of "None" implicitly if no other default value were specified:
def f(a: float, b: Optional[int]) -> float:
If "?" appended to a variable name could be defined similarly to Optional[int] as a shorthand for = None as well, you'd have even shorter syntax to express the intent of an "optional argument":
def f(a: float, b?: int) -> float:
and it would still be "optional" but with less typing...
I like and use TypeScript quite a bit... But I think we should avoid this because Python is more about being explicit than implicit (i.e. self argument in instance methods). So having to type =None may be less convenient but it makes it clear what the default value is for an optional argument. Anything like having a question mark would be changing the language quite a bit. It would probably be preferred to introduce a new (no-op) keyword like 'optional' or 'opt', but I don't think that's a very good idea either because then there's less available words for identifiers, or it could affect BC. Another thing is that Python functions have a set number of arguments based on the signature whereas JavaScript functions can accept any amount (TypeScript can stop this at compile time). It would be a large change if this were introduced: def f(a: Optional[int]) # f() or f(1) are both valid What should the default value for a be? Should it be defined and be set to None or it should it be undefined? In Python (unlike JavaScript) that would mean any mention of 'a' would be invalid, raising NameError. Which would have major implications on how to structure the function. Andrew