-4

I have a function that defines two lists, and I'd like to refer to one of these lists based on the variable passed as an argument. Note that I cannot pass the list as an argument since the list doesn't exist yet.

Is it simple to do in python? or I should create the lists outside and pass them as arguments for the sake of simplicity

def move(self,to_send):
    photos = ['example1']
    videos = ['example2']
    for file in @to_send: #where @to_send is either 'photos' or 'movies'
        ...

if whatever:
    move('photos')
else:
    move('videos')

EDIT: In order to avoid eval to transform the string into a list, I could do

def move(self,to_send):
    photos = ['example1']
    videos = ['example2']
    if to_send == 'photos':
        to_send = photos
    else:
        to_send = videos
    for file in to_send:
        ...
GWorking
  • 4,011
  • 10
  • 49
  • 90
  • 1
    Example please , this is unclear :)) – Abdelrahman Elkady Jan 20 '15 at 22:34
  • In what way is separately defining method `move(self,typename)` doing `move('photos')` then `move('videos')` a problem? It's simple, it's clear, it's just as performant. – smci Jan 20 '15 at 22:42
  • 3
    By the way, [don't use "type" as a variable name in Python](http://stackoverflow.com/questions/10568087/is-it-safe-to-use-the-python-word-type-in-my-code), it's a [builtin](https://docs.python.org/2/library/functions.html#type) – smci Jan 20 '15 at 22:43

3 Answers3

4
def move(self,type):
    photos = ['example1']
    movies = ['example2']
    for file in locals()[type]:
           ...

move("photos")

much better would be to keep a list of lists

my_lists = {
   "movies":...
   "photos":...
}

variable="movies"
do_something(my_lists[variable])
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

You are trying to pass strings instead of variable, so use eval

photos = [] # photos here
videos = [] # videos here

def move(type):
    for file in eval(type):
        print file

move('photos')
move('videos')
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • 7
    as a general rule of thumb `eval` is never the correct answer – Joran Beasley Jan 20 '15 at 22:39
  • But Gerard is passing strings, so it's not a matter of right or wrong. It's preference. – Malik Brahimi Jan 20 '15 at 22:41
  • I would argue it is always a matter of right or wrong ... but that said ... in theory he is providing the strings from a trusted source as such it *might* not be horribly aweful – Joran Beasley Jan 20 '15 at 22:43
  • I'd say that eval would be the solution. In other languages (I'd say this was perl or javascript?) I can directly eval the name without having to use the function eval (which always seems dangerous or inefficient). If in python this is the way to do it, I'll try to re-write the code to avoid it – GWorking Jan 20 '15 at 22:45
  • Well, I completely agree. I would pass variables instead, but as they say `'The customer always comes first.'` – Malik Brahimi Jan 20 '15 at 22:47
  • I've added a possible workaround in the question, which avoids eval and does the trick I suppose – GWorking Jan 20 '15 at 22:53
  • @Gerard just use `locals()["videos"]` ... you are overcomplicating the problem ... (or better yet make the lists live in a dictionary) – Joran Beasley Jan 20 '15 at 22:56
  • Guys, no need for the criticism. I'm addressing Gerard's question, not your impulse to write Pythonic code. I fall in that category by the way, but I'm hanging in there for Gerard. – Malik Brahimi Jan 20 '15 at 22:58
  • Indeed Joran, I've realised that I can define the list outside the function, so that I can pass it as an argument (i.e. no overcomplications :). The question as it is asked though does require eval, as Malik answered. – GWorking Jan 20 '15 at 23:12
  • You do realize `locals()['videos']` is the same as `eval('videos')`. I don't see what the fuss is about. – Malik Brahimi Jan 20 '15 at 23:15
0

Seems like you could just use a dictionary?

def move(to_send):
    d = {
        'photos' : ['example1']
        'videos' : ['example2']
    }
    for file in d[to_send]: #where @to_send is either 'photos' or 'movies'
        ...

if whatever:
    move('photos')
else:
    move('videos')

Or better yet, just pass "whatever" into the function?

def move(whatever):
    if whatever:
        media = ['example1']
    else:
        media = ['example2']
    for file in media:
        ...
move(whatever)
Fred S
  • 985
  • 4
  • 11