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
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