【发布时间】:2015-01-21 09:00:16
【问题描述】:
我有一个定义两个列表的函数,我想根据作为参数传递的变量来引用其中一个列表。请注意,我不能将列表作为参数传递,因为该列表尚不存在。
用python做起来简单吗?或者为了简单起见,我应该在外面创建列表并将它们作为参数传递
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')
编辑: 为了避免 eval 将字符串转换为列表,我可以这样做
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:
...
【问题讨论】:
-
请举例,这个不清楚:))
-
以什么方式单独定义方法
move(self,typename)做move('photos')然后move('videos')有问题?很简单,很清楚,性能一样。
标签: python list variables eval