How to assign a default constant value in a function declaration
Scott David Daniels
Scott.Daniels at Acm.Org
Mon Apr 5 13:50:39 EDT 2004
Marco Bartel wrote:
> rzed wrote:
>
>> "Vineet Jain" <vineet at eswap.com> wrote in
>> news:mailman.341.1081121191.20120.python-list at python.org:
>>
>>> The following does not work although it seems like something you
>>> should be able to do.
>>>
>>> def someFunction(option=Constants.DEFAULT_VALUE):
>>>
I suspect what the OP wants is to evaluate "Constants.DEFAULT_VALUE"
at function call time, not function definition time.
Indeed, something like the following does not work:
def someFunction(option=Constants.DEFAULT_VALUE):
print 'the option was', option
class Constants:
DEFAULT_VALUE = 13
someFunction()
In fact:
class Constants:
DEFAULT_VALUE = 13
def someFunction(option=Constants.DEFAULT_VALUE):
print 'the option was', option
Constants.DEFAULT_VALUE = 7 # get luckier
someFunction()
prints 13, not the possibly desired 7.
>> Do you mean in a context like this?
>> class Const:
>> someVal=255
>> otherVal=0
>>
>> def blip(Const.someVal):
Should be:
def blip(test=Const.someVal):
>
> i checked this out, and i think its the name you were using:
> Const
Nope, it is the missing arg name.
--
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list