简体   繁体   English

将实例作为函数参数传递

[英]Pass instance as function argument

I wrote a nice little app that gets Yahoo weather info and posts it to Twitter. 我写了一个不错的小应用程序,获取了Yahoo的天气信息并将其发布到Twitter。 It worked flawlessly and now I want to rearrange the code into differently named files so it makes more sense. 它运行得很完美,现在我想将代码重新排列到名称不同的文件中,这样更有意义。 And that's when I hit some issues. 那就是我遇到一些问题的时候。

Previously, I had a Class in libtweather.py . 以前,我在libtweather.py有一个Class It was my account . 那是我的account It allowed me to do accountName.parseFeed() and I'd get as output the parsed Yahoo weather. 它允许我执行accountName.parseFeed()并获得解析后的Yahoo天气的输出。 ( __ini__ took the weather URL, twitter username and password as args) __ini__将天气URL,Twitter用户名和密码作为args)

This was accessed from my main script which created instances of the Class like this: 这是从我的主脚本访问的,该脚本创建了Class实例,如下所示:
exec '%s = lw.twitterWeather("%s", "%s", "%s")' % (item[0], item[1], item[2], item[3]) It kept a list of all account names in a list which was passed as argument to the other functions. exec '%s = lw.twitterWeather("%s", "%s", "%s")' % (item[0], item[1], item[2], item[3])列表中所有帐户名称的列表,该列表作为参数传递给其他函数。

Another function getWeather got weather by doing: 另一个功能getWeather通过执行以下getWeather获得了天气:

def getWeather(accountList): #account names passed as a list of strings
    for item in accountList:
        print item, ': ',
        item = eval(item)       
        print item.parseFeed(), '\n

I've decided now to move the getWeather function to the same file as the Class but the line item = eval(item) 's giving me problems because there are no instances created in that file. 我现在决定将getWeather函数移到与Class相同的文件上,但是line item = eval(item)给我带来了问题,因为在该文件中没有创建任何实例。 All of them are in the main script. 所有这些都在主脚本中。

Now my question: Is there some way I could give those instances as arguments to the function? 现在我的问题是:有什么办法可以将这些实例作为函数的参数? Or must I put the function into the Class ? 还是必须将函数放入Class Even if I did that, I'd still need to do the item.parseFeed() for multiple items in the list so I'd still need the item = eval(item) , no? 即使我这样做了,我仍然需要对列表中的多个项目执行item.parseFeed() ,所以我仍然需要item = eval(item) ,不是吗?

Thanks in advance. 提前致谢。 My app's a tad bit to post here in entirety, but I'll post more code if needed to understand better. 我的应用程序有点完整地发布在这里,但是如果需要更好地理解,我会发布更多代码。

Update: I ended up running my libtweather.py to create instances when it's imported so that the functions inside it can access them (added the instance generating code at the bottom of the script). 更新:我最终在导入libtweather.py时运行libtweather.py来创建实例,以便其内部的函数可以访问它们(在脚本底部添加了实例生成代码)。 I'm sure there's a better way but it works for me currently and I'm OK with that. 我敢肯定有更好的方法,但是目前它对我有效,我对此表示满意。

You should be using an explicit dict for storing these items. 您应该使用明确的字典来存储这些项目。 eval , exec , globals , locals , and vars are all horribly silly ways to do this poorly. evalexecglobalslocalsvars都是糟糕的愚蠢方法。 Remember from the Zen of Python: "explicit is better than implicit." 请记住,从Python的禅宗中可以看出:“明确胜于隐含”。

feeds = {}
for item in whatever:
    feeds[item[0]] = lw.twitterWeather(*item[1:])

def getWeather(feeds, accountList):
    for item in accountList:
        print '%s: %s' % (item, feeds[item].parseFeed())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM