This should be a simple question...
python.list at tim.thechases.com
Mon Mar 9 16:31:04 CET 2009
Steven D'Aprano wrote:
> Tim Chase wrote:
>> If the constants don't actually share any conceptual commonality,
>> then SteveH is right, that they really should just be globals.
> Surely that's backwards? If the constants don't share any conceptual
> commonality, they should be kept independent in the functions and not made
> k = 2.5
> def population(n):
> """Return the total population of n families."""
> return k*n
> def calculate_leave(n):
> """Return the number of days of leave after working n days."""
> return n/10*k
This was in the example of two contants that were [un?]related to
each other, not one constant name for a pair of constant values
that happenstantially coincide (2.5 in your example). So
MEMBERS_PER_HOUSEHOLD = 2.5
DECI_HOURS_OF_LEAVE_ACCRUED_PER_DAY = 2.5
are unrelated to each other and would both just be plain ol'
globals (not just one combined global). However, if your
contants share a relation *with each other*, such as
LADEN = 42
UNLADEN = 45
DEAD = 0
LIVE = "VOOM!"
you they can be contained as non-global (only the AverageAirspeed
namespace becomes globalish) allowing for sharing constants
across function definitions
print "Being laden slows swallows by %s" % (
print "This parrot wouldn't %s if you put " \
"1000000 volts through it!" % AverageAirspeed.Parrot.LIVE
def bar(parrot, shopkeeper):
if parrot.speed == AverageAirspeed.Parrot.DEAD:
More information about the Python-list