question

Discussion in 'Python' started by Guest, Jan 22, 2008.

  1. Guest

    Guest Guest

    I'm still learning Python and was wanting to get some thoughts on this. I apologize if this sounds ridiculous... I'm mainly asking it to gain some knowledge of what works better. The main question I have is if I had a lot of lists to choose from, what's the best way to write the code so I'm not wasting a lot of memory? I've attempted to list a few examples below to hopefully be a little clearer about my question.

    Lets say I was going to be pulling different data, depending on what the user entered. I was thinking I could create a function which contained various functions inside:

    def albumInfo(theBand):
    def Rush():
    return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']

    def Enchant():
    return ['A Blueprint of the World', 'Wounded', 'Time Lost']

    ...

    The only problem with the code above though is that I don't know how to call it, especially since if the user is entering a string, how would I convert that string into a function name? For example, if the user entered 'Rush', how would I call the appropriate function --> albumInfo(Rush())

    But if I could somehow make that code work, is it a good way to do it? I'm assuming if the user entered 'Rush' that only the list in the Rush() function would be stored, ignoring the other functions inside the albumInfo() function?

    I then thought maybe just using a simple if/else statement might work like so:

    def albumInfo(theBand):
    if theBand == 'Rush':
    return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']
    elif theBand == 'Enchant':
    return ['A Blueprint of the World', 'Wounded', 'Time Lost']
    ...

    Does anyone think this would be more efficient?

    I'm not familiar with how 'classes' work yet (still reading through my 'Core Python' book) but was curious if using a 'class' would be better suited for something like this? Since the user could possibly choose from 100 or more choices, I'd like to come up with something that's efficient as well as easy to read in the code. If anyone has time I'd love to hear your thoughts.

    Thanks.

    Jay
    Guest, Jan 22, 2008
    #1
    1. Advertising

  2. On Jan 22, 7:58 pm, <> wrote:
    > I'm still learning Python and was wanting to get some thoughts on this.  I apologize if this sounds ridiculous...  I'm mainly asking it to gain some knowledge of what works better.  The main question I have is if I had a lot of lists to choose from, what's the best way to write the code so I'm not wasting a lot of memory?  I've attempted to list a few examples below to hopefully be a little clearer about my question.
    >
    > Lets say I was going to be pulling different data, depending on what the user entered.  I was thinking I could create a function which contained various functions inside:
    >
    > def albumInfo(theBand):
    >     def Rush():
    >         return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']
    >
    >     def Enchant():
    >         return ['A Blueprint of the World', 'Wounded', 'Time Lost']
    >
    >     ...
    >
    > The only problem with the code above though is that I don't know how to call it, especially since if the user is entering a string, how would I convert that string into a function name?  For example, if the user entered 'Rush', how would I call the appropriate function -->  albumInfo(Rush())
    >
    > But if I could somehow make that code work, is it a good way to do it?  I'm assuming if the user entered 'Rush' that only the list in the Rush() function would be stored, ignoring the other functions inside the albumInfo() function?
    >
    > I then thought maybe just using a simple if/else statement might work like so:
    >
    > def albumInfo(theBand):
    >     if theBand == 'Rush':
    >         return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']
    >     elif theBand == 'Enchant':
    >         return ['A Blueprint of the World', 'Wounded', 'Time Lost']
    >     ...
    >
    > Does anyone think this would be more efficient?
    >
    > I'm not familiar with how 'classes' work yet (still reading through my 'Core Python' book) but was curious if using a 'class' would be better suited for something like this?  Since the user could possibly choose from 100 or more choices, I'd like to come up with something that's efficient as well as easy to read in the code.  If anyone has time I'd love to hear your thoughts.
    >
    > Thanks.
    >
    > Jay


    What you want is a dictionary:

    albumInfo = {
    'Rush': 'Rush', 'Fly By Night', 'Caress of Steel',
    '2112', 'A Farewell to Kings', 'Hemispheres'],
    'Enchant': ['A Blueprint of the World',
    'Wounded', 'Time Lost'],
    ...
    }

    then to find the info just do:

    >>> albumInfo['Enchant']

    ['A Blueprint of the World', 'Wounded', 'Time Lost']

    It also makes it easy to add a new album on the fly:

    >>> albumInfo["Lark's tongue in Aspic"] = [ ... ]


    Hope that helps.

    --
    Arnaud
    Arnaud Delobelle, Jan 22, 2008
    #2
    1. Advertising

  3. Hi there :)

    A little tip upfront: In the future you might want to come up with a
    more descriptive subject line. This will help readers decide early if
    they can possibly help or not.

    wrote:
    > def albumInfo(theBand):
    > def Rush():
    > return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']
    >
    > def Enchant():
    > return ['A Blueprint of the World', 'Wounded', 'Time Lost']
    >
    > ...
    >

    Yuck! ;)


    > The only problem with the code above though is that I don't know how to call it, especially since if the user is entering a string, how would I convert that string into a function name?

    While this is relatively easy, it is *waaayyy* too complicated an
    approach here, because . . .


    > def albumInfo(theBand):
    > if theBand == 'Rush':
    > return ['Rush', 'Fly By Night', 'Caress of Steel', '2112', 'A Farewell to Kings', 'Hemispheres']
    > elif theBand == 'Enchant':
    > return ['A Blueprint of the World', 'Wounded', 'Time Lost']
    > ...
    >

    .. . . this is a lot more fitting for this problem.

    You could also have used a dictionary here, but the above is better if
    you have a lot of lists, because only the one you use is created (I
    think . . .).

    You might also want to consider preparing a textfile and reading it into
    a list (via lines = open("somefile.txt").readlines()) and then work with
    that so you don't have to hardcode it into the program. This however is
    somewhat advanced (if you're just starting out), so don't sweat it.



    > I'm not familiar with how 'classes' work yet (still reading through my 'Core Python' book) but was curious if using a 'class' would be better suited for something like this? Since the user could possibly choose from 100 or more choices, I'd like to come up with something that's efficient as well as easy to read in the code. If anyone has time I'd love to hear your thoughts.
    >

    Think of classes as "models of things and their behavior" (like an
    animal, a car or a robot). What you want is a simple "request->answer"
    style functionality, hence a function.


    Hope that helps.
    Happy coding :)

    /W
    Wildemar Wildenburger, Jan 22, 2008
    #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. sean
    Replies:
    1
    Views:
    574
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    637
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    930
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    407
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    820
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page