[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484
Gregory P. Smith
greg at krypto.org
Wed Aug 3 10:57:29 EDT 2016
On Wed, Aug 3, 2016, 6:49 AM Sven R. Kunze <srkunze at mail.de> wrote:
> On 01.08.2016 23:31, Guido van Rossum wrote:
> > PEP 484 doesn't change Python's syntax. Therefore it has no good
> > syntax to offer for declaring the type of variables, and instead you
> > have to write e.g.
> >
> > a = 0 # type: float
> > b = [] # type: List[int]
> > c = None # type: Optional[str]
> >
> > I'd like to address this in the future, and I think the most elegant
> > syntax would be to let you write these as follows:
> >
> > a: float = 0
> > b: List[int] = []
> > c: Optional[str] = None
> >
> > (I've considered a 'var' keyword in the past, but there just are too
> > many variables named 'var' in my code. :-)
>
> I can't help but this seems like a short "key: value" dict declaration.
>
> Besides the fact that those examples don't really highlight the real
> use-cases. But others already covered that.
>
> > There are some corner cases to consider. First, to declare a
> > variable's type without giving it an initial value, we can write this:
> >
> > a: float
>
> Then writing "a" should be allow, too, right? As a variable declaration
> without any type hint. That's usually a NameError.
>
> Put it differently, what is the use-case Python has missed so long in
> not providing a way of declaring an empty variable? Will it be filled
> with None?
>
Nope, it wouldn't do anything at all. No bytecode. No namespace updated. No
None. It is merely an informational statement that an optional type checker
pass may make use of.
This definitely counts as a difference between a bare 'a' and 'a: SPAM'.
The former is a name lookup while the latter is a no-op. Good or bad I'm
undecided. At least the latter is useful while the former is more of a side
effect of how Python works.
> > Second, when these occur in a class body, they can define either class
> > variables or instance variables. Do we need to be able to specify
> > which?
>
> Could be useful but might result in a lot of double maintenance work
> (class body + place of initilization).
>
> > Third, there's an annoying thing with tuples/commas here. On the one
> > hand, in a function declaration, we may see (a: int = 0, b: str = '').
> > On the other hand, in an assignment, we may see
> >
> > a, b = 0, ''
> >
> > Suppose we wanted to add types to the latter. Would we write this as
> >
> > a, b: int, str = 0, ''
> >
> > or as
> >
> > a: int, b: str = 0, ''
> >
> > ??? Personally I think neither is acceptable, and we should just write
> it as
> >
> > a: int = 0
> > b: str = ''
> >
> > but this is a slight step back from
> >
> > a, b = 0, '' # type: (int, str)
> >
>
> Or using "a: float" from above:
>
> a: float
> b: str
> a, b = 0, ''
>
>
> So, ignoring commas for now and solving it later would also work.
>
> Sven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160803/54744d5b/attachment.html>
More information about the Python-ideas
mailing list