Constants [was Re: newb question about @property]

Steve D'Aprano steve+python at pearwood.info
Thu Oct 5 00:32:50 EDT 2017


On Thu, 5 Oct 2017 04:00 am, Paul Moore wrote:


> I wonder - would the people who want "real constants" find the
> following confusing:
> 
>>>> from demo import foo
>>>> foo = 14
>>>> foo
> 14
> 
> It's fundamental to the way the import function works, and how names
> in Python behave, but I can see someone with a background in other
> languages with "real" constants thinking "but foo is a constant, and
> importing it stops it being a constant!"

Of course it would be confusing. Just as "from module import foo" can be
confusing today, without constants. People are surprised by at least two
things:

- if foo is mutable, they may be surprised that importing it doesn't 
  make a copy; mutating the "imported copy" will show up everywhere;

- they may be surprised that the local name "foo" isn't an alias to
  the qualified name "demo.foo": if demo.foo changes, foo does not.


So is early binding of function defaults. And in other contexts, so is late
binding of function defaults.

Scoping and name binding rules are something one has to learn. When I first
learned Python, they caused me trouble, and I'm sure they will cause any
beginner to programming trouble. Adding constants to the language won't
change that.

Besides, if we had constants:

const foo = 1234

then we could have:

from demo import const foo



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list