Function return a dictionary

Discussion in 'Python' started by Boris Mok, Oct 1, 2007.

  1. Boris Mok

    Boris Mok Guest

    Hi all,

    I'm doing a function which needs return an arrary -- or more specially a
    dictionary data type.
    I have a sample like this

    def AFC():
    v["a"] = 1
    return v

    v = AFC()
    print v["a"]

    with error
    NameError: global name 'v' is not defined

    how do I go about it?

    Thanks in advance.
    Boris Mok
     
    Boris Mok, Oct 1, 2007
    #1
    1. Advertising

  2. Boris Mok

    Ben Finney Guest

    Boris Mok <> writes:

    > I'm doing a function which needs return an arrary -- or more specially
    > a dictionary data type.


    Yes. Python doesn't have an "array" type natively, and it's confusing
    to refer to a dict as an array because there *are* "array"s in PyNum.

    > I have a sample like this
    >
    > def AFC():
    > v["a"] = 1
    > return v


    Your function never specifies where 'v' comes from. So, when you first
    attempt to access an item from 'v' as an existing dict, you get a
    NameError.

    If you want to create 'v' inside the function, you'll need to do so
    before attempting to use it.

    def foo():
    bar = {"spam": 1}
    return bar

    cheeseburger = foo()
    print cheeseburger["spam"]

    To get a thorough grounding in basic concepts like this, please work
    through the Python tutorial <URL:http://docs.python.org/tut/>, from
    beginning to end, running every example and experimenting until you
    understand why it does what it does, before moving onto the next.

    --
    \ "[T]he question of whether machines can think [...] is about as |
    `\ relevant as the question of whether submarines can swim." |
    _o__) —Edsger W. Dijkstra |
    Ben Finney
     
    Ben Finney, Oct 1, 2007
    #2
    1. Advertising

  3. Boris Mok

    Duncan Booth Guest

    Boris Mok <> wrote:

    > Hi all,
    >
    > I'm doing a function which needs return an arrary -- or more specially a
    > dictionary data type.
    > I have a sample like this
    >
    > def AFC():
    > v["a"] = 1
    > return v
    >
    > v = AFC()
    > print v["a"]
    >
    > with error
    > NameError: global name 'v' is not defined
    >
    > how do I go about it?
    >

    At some point you have to initialise v to a dictionary, Python isn't going
    to guess for you. e.g.

    def AFC():
    v = {}
    v["a"] = 1
    return v
     
    Duncan Booth, Oct 1, 2007
    #3
    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. Ilias Lazaridis
    Replies:
    6
    Views:
    480
    Ilias Lazaridis
    Feb 21, 2006
  2. Greenhorn
    Replies:
    15
    Views:
    880
    Keith Thompson
    Mar 6, 2005
  3. james_027
    Replies:
    1
    Views:
    361
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Navkirat Singh
    Replies:
    6
    Views:
    3,325
    Navkirat Singh
    Jul 29, 2010
  5. JT

    return dictionary from a function

    JT, Sep 29, 2003, in forum: ASP General
    Replies:
    1
    Views:
    130
    Mark Schupp
    Sep 29, 2003
Loading...

Share This Page