S
sidRo
How to declare a constant in python 3?
How to declare a constant in python 3?
There aren't true constants in Python, but instead we use a standard
defined by PEP 8, which states constants are in all caps, for example,
PI = 3.14, as opposed to pi = 3.14 which could change (according to
PEP 8, that is)
If you mean creating a binding which can't be re-bound: −1.
The ability to re-bind any attribute, even ones which the author thought
should be constant, makes writing unit tests much easier. I don't see
that putative benefits of constant bindings would be anywhere near as
valuable.
Of course, it is just my personal opinion. It might be not pythonic,
I may be wrong, yet - concept of constants is not something new and
if other languages, like C/C++/Java/Perl/ (bash even) have them,
I can't see the reason not to have them in Python.
[...]You can have them in Python. Just run your code through cpp (the C
preprocessor) first. Voila!
It's handy for other things too. Don't like Python's lack of "then"
and "end if"?
2011/6/24 Waldek M. said:Dnia Fri, 24 Jun 2011 01:29:38 +1000, Chris Angelico napisa³(a):[...]You can have them in Python. Just run your code through cpp (the C
preprocessor) first. Voila!
It's handy for other things too. Don't like Python's lack of "then"
and "end if"?
Yup, got the sarcasm, that's for sure.
But your point was...?
Waldek M. said:But your point was...?
I don't see how that's relevant. I'm referring to run-time re-binding of
attributes, without the need for changing the code.
Right. Which often involves special set up of the environment in which
that code runs, in order to conduct a proper test.
If the test I need to perform involves demonstrating what the code under
test will do with different values for a name you consider constant,
then I have Python's ability to re-bind that name at run-time.
And I, as the user of your code, say that you don't get to dictate how I
use it. I will take the authors's recommendations under advisement, but
I reject any authority to restrict what I can do with it.
Unit tests are just an obvious example. The real lesson here is that the
reciient of the code you write can, and probably will, use it in ways
you never expected. It's not for you to decide which of those uses are
permitted.
Right. One useful phrase to remember from the founder of the Python
language is that “We're all consenting adults hereâ€.
Indicate through the API and its documentation how you recommend I use
your code; but treat me as an adult capable of making my own decisions
about what risks I accept. Understand that I will be using the code you
wrote in the way I see fit.
That it's easier for you to find ways to achieve what you want than it
is require Python to change to accommodate your need.
That if you want something, there's usually a way to get it.
Sometimes, giving someone what they want - or showing them how to get
it - makes it obvious to them whether or not they really want it.
Chris Angelico
2011/6/25 Waldek M. said:Dnia Fri, 24 Jun 2011 08:00:06 +1000, Chris Angelico napisa³(a):
Usually, it does. But presenting me with a fake and
obviously-not-logical-and-useful version of constants didn't really
say anything about why is having constants such a bad idea
Just like presenting me with a rotten apple doesn't prove
anything general about that fruit.
In all seriousness, sometimes adding features to one language is best
done by dropping to another. This is probably not as useful in
interpreted languages like Python, but I have on multiple occasions
run code through the C preprocessor as part of my compilation process,
as per my example.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.