469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How to assign a default constant value in a function declaration

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

def someFunction(option=Constants.DEFAULT_VALUE):

Thanks,

V

Jul 18 '05 #1
8 1994
Vineet Jain wrote:
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
Jul 18 '05 #2
On Sun, 4 Apr 2004 19:26:30 -0400, Vineet Jain wrote:
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.

--
\ "I took a course in speed waiting. Now I can wait an hour in |
`\ only ten minutes." -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
In article <ma**************************************@python.o rg>,
"Vineet Jain" <vi****@eswap.com> wrote:
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.
Jul 18 '05 #4
"Vineet Jain" <vi****@eswap.com> wrote in
news:ma**************************************@pyth on.org:
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?
class Const: .... someVal=255
.... otherVal=0
.... Const.someVal 255 someVal=255
someVal 255 def blip(Const.someVal): File "<stdin>", line 1
def blip(Const.someVal):
^
SyntaxError: invalid syntax def blip(someVal):

.... (no syntax error)
I've wondered about that, too.
--
rzed

Jul 18 '05 #5
rzed wrote:
"Vineet Jain" <vi****@eswap.com> wrote in
news:ma**************************************@pyth on.org:
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?
class Const: ... someVal=255
... otherVal=0
... Const.someVal 255 someVal=255
someVal 255 def blip(Const.someVal): File "<stdin>", line 1
def blip(Const.someVal):
^
SyntaxError: invalid syntax def blip(someVal): ... (no syntax error)
I've wondered about that, too.


Stop wondering then:
class Constants: .... DEFAULT_VALUE = 42
.... def someFunction(option=Constants.DEFAULT_VALUE): .... print "option =", option
.... someFunction(1) option = 1 someFunction() option = 42 Constants.DEFAULT_VALUE = "another value"
someFunction() option = 42 Constants = None
someFunction()

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

Jul 18 '05 #6
rzed wrote:
"Vineet Jain" <vi****@eswap.com> wrote in
news:ma**************************************@pyth on.org:

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?

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


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

Jul 18 '05 #7
Marco Bartel wrote:
rzed wrote:
"Vineet Jain" <vi****@eswap.com> wrote in
news:ma**************************************@pyth on.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
Sc***********@Acm.Org
Jul 18 '05 #8
Scott David Daniels <Sc***********@Acm.Org> wrote in
news:40******@nntp0.pdx.net:
Marco Bartel wrote:
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.

--
rzed

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by lovecreatesbeauty | last post: by
7 posts views Thread by shikn | last post: by
6 posts views Thread by Fuzzyman | last post: by
11 posts views Thread by webgour | last post: by
3 posts views Thread by Dan Smithers | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.