default variable in python $_

Discussion in 'Python' started by rh0dium, Oct 10, 2006.

  1. rh0dium

    rh0dium Guest

    Hi all,

    So I have this simple little routine.. say like this..


    def foo()
    return {"a":"b", "b":"c"}

    if foo():
    print "Have foo"


    Now I want the dictionary item a (ie. b)

    How can I do it the above way or do I still have to go like this..

    def foo()
    return {"a":"b", "b":"c"}

    z = foo()
    if z:
    print "Have foo"
    print z['a']

    This is where $_ in perl is awesome - There must be a default variable
    in python right?
    rh0dium, Oct 10, 2006
    #1
    1. Advertising

  2. rh0dium

    Georg Brandl Guest

    rh0dium wrote:
    > Hi all,
    >
    > So I have this simple little routine.. say like this..
    >
    >
    > def foo()
    > return {"a":"b", "b":"c"}
    >
    > if foo():
    > print "Have foo"
    >
    >
    > Now I want the dictionary item a (ie. b)
    >
    > How can I do it the above way or do I still have to go like this..
    >
    > def foo()
    > return {"a":"b", "b":"c"}
    >
    > z = foo()
    > if z:
    > print "Have foo"
    > print z['a']
    >
    > This is where $_ in perl is awesome - There must be a default variable
    > in python right?


    Why should there? When you're learning a new language, the first thing you
    should do is to empty your mind and stop expecting concepts known from other
    languages. Instead, try to grasp what the essence of the new language is.

    In the case of Python, one credo is "explicit is better than implicit".
    IMO, this precludes, among other things, the notion of a "default variable".

    Where is the problem with your second snippet?

    Georg
    Georg Brandl, Oct 10, 2006
    #2
    1. Advertising

  3. rh0dium

    Guest

    rh0dium:
    > This is where $_ in perl is awesome - There must be a default variable
    > in python right?


    A default variable may add bugs to your code, and newbies of the
    language may see it coming from air, so Python avoids such things. The
    only Python "default variable" I know of is the _ that when used in the
    Python shell, it (seem to) stores the last not-None result (inside
    scripts it is a normal name sometimes used to 'ignore' some values):

    >>> a = 1
    >>> _

    Traceback (most recent call last):
    ...
    NameError: name '_' is not defined
    >>> print 2

    2
    >>> _

    Traceback (most recent call last):
    ...
    NameError: name '_' is not defined
    >>> 5 / 3

    1
    >>> _

    1
    >>> None
    >>> _

    1
    >>> 2

    2
    >>> _

    2

    Bye,
    bearophile
    , Oct 11, 2006
    #3
  4. rh0dium

    MaR Guest

    rh0dium wrote:
    > Hi all,
    >
    > So I have this simple little routine.. say like this..
    >
    >
    > def foo()
    > return {"a":"b", "b":"c"}
    >
    > if foo():
    > print "Have foo"
    >
    >
    > Now I want the dictionary item a (ie. b)
    >
    > How can I do it the above way or do I still have to go like this..
    >
    > def foo()
    > return {"a":"b", "b":"c"}
    >
    > z = foo()
    > if z:
    > print "Have foo"
    > print z['a']
    >
    > This is where $_ in perl is awesome - There must be a default variable
    > in python right?


    As said in earlier response, such a default variable is *dangerous* at
    best!
    Not knowing much about Perl and guessing that the $_ is a global
    storage space, my immediate thought is; What happens if you have
    multiple threads?

    The example is too simplified to give any clues as to what you are
    needing the feature for.
    Guessing that you want to generate some dict() and use the result once
    and then discard, write:

    foo_default = 1
    def foo(): return(generate_some_dict())

    you can further write

    foo().get('a', foo_default)

    giving compact code and ensuring that you get a well defined result
    regardless what dictionary keys there are.
    MaR, Oct 11, 2006
    #4
  5. rh0dium

    rh0dium Guest

    Hi Maria,

    This is exactly what I was looking for. I (as others have asked me to)
    cleared my head of the other languages, but was mearly giving perl as
    an example indicating the compactness I was after.

    Thanks Maria!!

    MaR wrote:
    > rh0dium wrote:
    > > Hi all,
    > >
    > > So I have this simple little routine.. say like this..
    > >
    > >
    > > def foo()
    > > return {"a":"b", "b":"c"}
    > >
    > > if foo():
    > > print "Have foo"
    > >
    > >
    > > Now I want the dictionary item a (ie. b)
    > >
    > > How can I do it the above way or do I still have to go like this..
    > >
    > > def foo()
    > > return {"a":"b", "b":"c"}
    > >
    > > z = foo()
    > > if z:
    > > print "Have foo"
    > > print z['a']
    > >
    > > This is where $_ in perl is awesome - There must be a default variable
    > > in python right?

    >
    > As said in earlier response, such a default variable is *dangerous* at
    > best!
    > Not knowing much about Perl and guessing that the $_ is a global
    > storage space, my immediate thought is; What happens if you have
    > multiple threads?
    >
    > The example is too simplified to give any clues as to what you are
    > needing the feature for.
    > Guessing that you want to generate some dict() and use the result once
    > and then discard, write:
    >
    > foo_default = 1
    > def foo(): return(generate_some_dict())
    >
    > you can further write
    >
    > foo().get('a', foo_default)
    >
    > giving compact code and ensuring that you get a well defined result
    > regardless what dictionary keys there are.
    rh0dium, Oct 11, 2006
    #5
    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. micky

    default.mspx or default.do

    micky, Dec 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    Andrew Robinson
    Dec 17, 2005
  2. mfglinux
    Replies:
    11
    Views:
    670
    Roberto Bonvallet
    Sep 12, 2007
  3. David Filmer
    Replies:
    19
    Views:
    217
    Kevin Collins
    May 21, 2004
  4. Chris Angelico
    Replies:
    27
    Views:
    261
    Mark Lawrence
    Mar 5, 2013
  5. Gene Heskett
    Replies:
    0
    Views:
    96
    Gene Heskett
    Mar 5, 2013
Loading...

Share This Page