question

G

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
 
A

Arnaud Delobelle

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:
['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.
 
W

Wildemar Wildenburger

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top