<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Aug 3, 2016, 6:49 AM Sven R. Kunze <<a href="mailto:srkunze@mail.de">srkunze@mail.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 01.08.2016 23:31, Guido van Rossum wrote:<br>
> PEP 484 doesn't change Python's syntax. Therefore it has no good<br>
> syntax to offer for declaring the type of variables, and instead you<br>
> have to write e.g.<br>
><br>
> a = 0 # type: float<br>
> b = [] # type: List[int]<br>
> c = None # type: Optional[str]<br>
><br>
> I'd like to address this in the future, and I think the most elegant<br>
> syntax would be to let you write these as follows:<br>
><br>
> a: float = 0<br>
> b: List[int] = []<br>
> c: Optional[str] = None<br>
><br>
> (I've considered a 'var' keyword in the past, but there just are too<br>
> many variables named 'var' in my code. :-)<br>
<br>
I can't help but this seems like a short "key: value" dict declaration.<br>
<br>
Besides the fact that those examples don't really highlight the real<br>
use-cases. But others already covered that.<br>
<br>
> There are some corner cases to consider. First, to declare a<br>
> variable's type without giving it an initial value, we can write this:<br>
><br>
> a: float<br>
<br>
Then writing "a" should be allow, too, right? As a variable declaration<br>
without any type hint. That's usually a NameError.<br>
<br>
Put it differently, what is the use-case Python has missed so long in<br>
not providing a way of declaring an empty variable? Will it be filled<br>
with None?<br></blockquote></div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> Second, when these occur in a class body, they can define either class<br>
> variables or instance variables. Do we need to be able to specify<br>
> which?<br>
<br>
Could be useful but might result in a lot of double maintenance work<br>
(class body + place of initilization).<br>
<br>
> Third, there's an annoying thing with tuples/commas here. On the one<br>
> hand, in a function declaration, we may see (a: int = 0, b: str = '').<br>
> On the other hand, in an assignment, we may see<br>
><br>
> a, b = 0, ''<br>
><br>
> Suppose we wanted to add types to the latter. Would we write this as<br>
><br>
> a, b: int, str = 0, ''<br>
><br>
> or as<br>
><br>
> a: int, b: str = 0, ''<br>
><br>
> ??? Personally I think neither is acceptable, and we should just write it as<br>
><br>
> a: int = 0<br>
> b: str = ''<br>
><br>
> but this is a slight step back from<br>
><br>
> a, b = 0, '' # type: (int, str)<br>
><br>
<br>
Or using "a: float" from above:<br>
<br>
a: float<br>
b: str<br>
a, b = 0, ''<br>
<br>
<br>
So, ignoring commas for now and solving it later would also work.<br>
<br>
Sven<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>