How to assign a default constant value in a function declaration

Discussion in 'Python' started by Vineet Jain, Apr 5, 2004.

  1. Vineet Jain

    Vineet Jain Guest

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

    def someFunction(option=Constants.DEFAULT_VALUE):

    Thanks,

    V
    Vineet Jain, Apr 5, 2004
    #1
    1. Advertising

  2. Vineet Jain

    Peter Hansen Guest

    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
    Peter Hansen, Apr 5, 2004
    #2
    1. Advertising

  3. Vineet Jain

    Ben Finney Guest

    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/>
    Ben Finney, Apr 5, 2004
    #3
  4. Vineet Jain

    Mel Wilson Guest

    In article <>,
    "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.
    Mel Wilson, Apr 5, 2004
    #4
  5. Vineet Jain

    rzed Guest

    "Vineet Jain" <> wrote in
    news::

    > 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
    rzed, Apr 5, 2004
    #5
  6. Vineet Jain

    Peter Otten Guest

    rzed wrote:

    > "Vineet Jain" <> wrote in
    > news::
    >
    >> 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
    Peter Otten, Apr 5, 2004
    #6
  7. Vineet Jain

    Marco Bartel Guest

    rzed wrote:
    > "Vineet Jain" <> wrote in
    > news::
    >
    >
    >>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
    Marco Bartel, Apr 5, 2004
    #7
  8. Marco Bartel wrote:

    > rzed wrote:
    >
    >> "Vineet Jain" <> wrote in
    >> news::
    >>
    >>> 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
    Scott David Daniels, Apr 5, 2004
    #8
  9. Vineet Jain

    rzed Guest

    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
    rzed, Apr 5, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    945
  2. Ovidesvideo
    Replies:
    4
    Views:
    479
    Andrey Tarasevich
    Dec 10, 2004
  3. Replies:
    4
    Views:
    1,049
    Richard Tobin
    Dec 12, 2006
  4. Bolin
    Replies:
    4
    Views:
    393
  5. InuY4sha

    constant function pointers declaration

    InuY4sha, Nov 24, 2009, in forum: C Programming
    Replies:
    6
    Views:
    1,429
    Richard Tobin
    Nov 25, 2009
Loading...

Share This Page