[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484

Daniel Moisset dmoisset at machinalis.com
Wed Aug 3 10:51:28 EDT 2016


On Tue, Aug 2, 2016 at 11:09 PM, Guido van Rossum <guido at python.org> wrote:

> On Mon, Aug 1, 2016 at 4:35 PM, Daniel Moisset <dmoisset at machinalis.com>
> wrote:
> >
> > I'd say that if I have a class C with a class variable cv, instance
> variable
> > iv, a good type checking system should detect:
> >
> > C.cv # ok
> > C.iv # error!
> > C().iv # ok
> >
> > which is something that PEP484 doesn't clarify much (and mypy flags all
> 3 as
> > valid)
>
> Yeah, this is all because you can't express that in Python either.
> When you see an assignment in a class body you can't tell if it's
> meant as an instance variable default or a class variable (except for
> some specific cases -- e.g. nested class definitions are pretty
> obvious :-).
>

May be I've gotten wrong my python style for many years, but I always
considered that the "proper" way to create instance variables was inside
the initializer (or in rare occasions, some other method/classmethod). For
me, an assignment at a class body is a class variable/constant.

So I was going to propose "type declarations at class level are always for
class variables, and inside methods (preceded by "self.")  are for instance
variables". Using class level variables for defaults always seemed
unpythonic and error prone (when mutable state is involved) to me. I felt
that was common practice but can't find documentation to support it, so I'd
like to hear if I'm wrong there :)



>
> --
Daniel F. Moisset - UK Country Manager
www.machinalis.com
Skype: @dmoisset
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160803/59ce5738/attachment.html>


More information about the Python-ideas mailing list