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
global.
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
class AverageAirspeed:
class Swallow:
LADEN = 42
UNLADEN = 45
class Parrot:
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
def foo():
print "Being laden slows swallows by %s" % (
AverageAirspeed.Swallow.UNLADEN -
AverageAirspeed.Swallow.LADEN)
print "This parrot wouldn't %s if you put " \
"1000000 volts through it!" % AverageAirspeed.Parrot.LIVE
def bar(parrot, shopkeeper):
answer(AverageAirspeed.Swallow.LADEN)
if parrot.speed == AverageAirspeed.Parrot.DEAD:
return shopkeeper(parrot)
-tkc
--
http://mail.python.org/mailman/listinfo/python-list