Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to assign a default constant value in a function declaration

Reply
Thread Tools

How to assign a default constant value in a function declaration

 
 
Vineet Jain
Guest
Posts: n/a
 
      04-04-2004
The following does not work although it seems like something you should be
able to do.

def someFunction(option=Constants.DEFAULT_VALUE):

Thanks,

V





 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-05-2004
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
 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      04-05-2004
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/>
 
Reply With Quote
 
Mel Wilson
Guest
Posts: n/a
 
      04-05-2004
In article <mailman.341.1081121191.20120.python->,
"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):


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

Regards. Mel.
 
Reply With Quote
 
rzed
Guest
Posts: n/a
 
      04-05-2004
"Vineet Jain" <> wrote in
news:mailman.341.1081121191.20120.python-:

> 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

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-05-2004
rzed wrote:

> "Vineet Jain" <> wrote in
> news:mailman.341.1081121191.20120.python-:
>
>> 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

 
Reply With Quote
 
Marco Bartel
Guest
Posts: n/a
 
      04-05-2004
rzed wrote:
> "Vineet Jain" <> wrote in
> news:mailman.341.1081121191.20120.python-:
>
>
>>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

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      04-05-2004
Marco Bartel wrote:

> rzed wrote:
>
>> "Vineet Jain" <> wrote in
>> news:mailman.341.1081121191.20120.python-:
>>
>>> 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

 
Reply With Quote
 
rzed
Guest
Posts: n/a
 
      04-05-2004
Scott David Daniels <> wrote in
news::

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
constant function pointers declaration InuY4sha C Programming 6 11-25-2009 09:58 PM
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57