By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

How to assign a default constant value in a function declaration

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.