How to assign a default constant value in a function declaration

V

Vineet Jain

The following does not work although it seems like something you should be
able to do.

def someFunction(option=Constants.DEFAULT_VALUE):

Thanks,

V
 
P

Peter Hansen

Vineet said:
The following does not work although it seems like something you should be
able to do.

def someFunction(option=Constants.DEFAULT_VALUE):

Please post the actual error traceback you are getting, and a
better description of "does not work". Your problem could be
just about anything at this point...

-Peter
 
B

Ben Finney

The following does not work although it seems like something you
should be able to do.

def someFunction(option=Constants.DEFAULT_VALUE):

That's not a question.

Is there something that isn't behaving as you expect? If so, please
explain what you expect, and what you're actually experiencing.
 
M

Mel Wilson

The following does not work although it seems like something you should be
able to do.

def someFunction(option=Constants.DEFAULT_VALUE):

Certainly seems like it. What's it doing to make you say
it's not working?

Regards. Mel.
 
R

rzed

The following does not work although it seems like something you
should be able to do.

def someFunction(option=Constants.DEFAULT_VALUE):
Do you mean in a context like this?
.... someVal=255
.... otherVal=0
.... File "<stdin>", line 1
def blip(Const.someVal):
^
SyntaxError: invalid syntax.... (no syntax error)


I've wondered about that, too.
 
P

Peter Otten

rzed said:
Do you mean in a context like this?

... someVal=255
... otherVal=0
...
File "<stdin>", line 1
def blip(Const.someVal):
^
SyntaxError: invalid syntax
... (no syntax error)


I've wondered about that, too.

Stop wondering then:
.... DEFAULT_VALUE = 42
........ print "option =", option
....option = 42

Here Constants might also be a module. The only constraint is that
Constants.DEFAULT_VALUE is bound when the function is defined, not when
it's called.

Peter
 
M

Marco Bartel

rzed said:
Do you mean in a context like this?



... someVal=255
... otherVal=0
...


File "<stdin>", line 1
def blip(Const.someVal):
^
SyntaxError: invalid syntax


... (no syntax error)


I've wondered about that, too.

i checked this out, and i think its the name you were using:

Const

i tried this, and it works fine
>class mconst:
> testval = 255
>def testme(test = mconst.testval)
> print test
>print testme()

255


if this is what you wanted dont use the word const for the class

CU
Marco
 
S

Scott David Daniels

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.


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.
 
R

rzed

Marco said:
rzed wrote:
[...]
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.

Well, not so much that as an incorrectly formed parameter name. I
can legally do this:
def blip( someVal ):
...

but not this:
def blip( x.someVal ):
=> SyntaxError aimed at the dot.

Since there is no argname to assign a value to, "Const.someVal" is
taken as an identifier for a passed-in parameter. But it seems
(sensibly enough) that an identifier can't contain a '.' character,
which evidently is reserved for a qualifier separator (or some such
term) in that context.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top