473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2112
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
3459
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
11
5254
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string>...
7
2564
by: shikn | last post by:
I wrote one simple piece of codes to test const objects in C++ as below: ================================= class Empty{}; main() { const Empty e1; } ==================================
5
2583
by: Jeff | last post by:
Dim lngWeights as long, lngBuckets as Long Const STDWEIGHTS = 12 lngBuckets = 1000 lngWeights = lngBuckets * STDWEIGHTS This causes an Overflow error. For lower values it works. Obviously...
6
24258
by: Fuzzyman | last post by:
What gives ? >>> a = >>> def f(): return a >>> f() >>> a.append(3) >>> f()
12
2301
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
11
1748
by: webgour | last post by:
Hello, I am wondering why the following works, on IE6, but with an error : "Not implemented". function TEST(){} TEST.prototype.Initialize = function() { var mImage = new Image(); var mDate...
9
8863
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
3
4750
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.