【发布时间】:2014-09-13 11:55:39
【问题描述】:
Python 中的动态属性访问?
我正在从事一个我决定进行的小型编程项目,但遇到了一个我不知道如何解决的错误......我已经搜索了答案,但实际上我只找到了似乎是的结果不适用于这种特定情况(或者我可能不是在寻找正确的东西......)。这是我正在尝试运行的代码(不包括完整的程序,它太长了):
def Bet(percent):
Screen = screenGrab()
print 'betting ' + percent
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
mousePos(Cord.percent)
leftClick()
time.sleep(.05)
leftClick()
BetNum = RNDORG.IntegerGenerator(1, 101)
if (BetNum < 51):
print 'Betting Left'
mousePos(Cord.Comp_Left)
leftClick()
time.sleep(.05)
leftClick()
if (BetNum >= 51):
print 'Betting Right'
mousePos(Cord.Comp_Right)
leftClick()
time.sleep(.05)
leftClick()
#(later maybe) calculate best option,click on a percent, click on a competitior
当我在 python 命令提示符下运行程序时,我得到这个错误:
Perecentage to bet? (format it as P_%, to go all in type ALLIN)P_10
bettingP_10
Traceback (most recent call last):
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 148, in <module>
main()
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 141, in main
Bet(UPercent)
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 112, in Bet
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
AttributeError: class Cord has no attribute 'percent'
对我来说最麻烦的部分是 python 似乎将 percent 参数解释为命令“percent”的字面意思,而不是实际传递给它的内容(在本例中为 P_10,如 print 语句所示)。我有一种方法可以处理通过 bet() 函数传递的内容,并将其限制为只有我有 Cord 参数的参数,我只是不确定如何让 python 在命令中不将“百分比”解释为“百分比”调用:
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
所以基本上我在问我如何使用这个参数来调用一个方法(如果那可能的话......)?我需要把它变成一个字符串还是什么?如果不可能,我如何解释参数以将其发送到 Cord 方法?
我对编程比较陌生,有些事情告诉我这不是我应该尝试的,或者应该有另一种解决方法。我考虑为每种情况创建一种 Switch 语句(类似于http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html),但我觉得我目前拥有的更有效。
这是我用来预过滤传递给参数的代码:
UPercent = raw_input('Perecentage to bet? (format it as P_%, to go all in type ALLIN)')
if (UPercent == ('P_10' or 'P_20' or 'P_30' or 'P_40' or 'P_50' or 'P_60' or 'P_70' or 'P_80' or 'P_90' or 'ALLIN')):
Bet(UPercent)
谢谢,欢迎回复!
好的,解决的代码如下(我还修复了“if ... ==”语句,但这不是我要问的)...:
def Bet(percent):
Screen = screenGrab()
if (Screen.getpixel(getattr(Cord, percent)) == Colors.percent_button):
#Check bets-----------------------------------------------------------------
print 'betting ' + percent
mousePos(getattr(Cord, percent))
leftClick()
time.sleep(.05)
leftClick()
BetNum = RNDORG.IntegerGenerator(1, 101)
if (BetNum < 51):
print 'Betting Left'
mousePos(Cord.Comp_Left)
leftClick()
time.sleep(.05)
leftClick()
if (BetNum >= 51):
print 'Betting Right'
mousePos(Cord.Comp_Right)
leftClick()
time.sleep(.05)
leftClick()
#(later maybe) calculate best option,click on a percent, click on a competitior
这里澄清一下绳子和颜色是什么:
class Cord:
Comp_Left = (730,(ymax-140)-y_pad)
Comp_Right = (1190,(ymax-140)-y_pad)
#CompLeftValue = (,)
#CompRightValue = (,)
TextBox = (864, 767)
P_10 = (220,(ymax-50)-y_pad) #button centers seem to have a 160-170 pixel interval, .085 rati
P_20 = (380,(ymax-50)-y_pad) #60 is the distance from the bottom of the screen to the buttons
P_30 = (540,(ymax-50)-y_pad) #on the windows operating system, so y max -60 will always give
P_40 = (710,(ymax-50)-y_pad) #the approximate y coordinate
P_50 = (870,(ymax-50)-y_pad)
P_60 = (1030,(ymax-50)-y_pad)
P_70 = (1200,(ymax-50)-y_pad)
P_80 = (1370,(ymax-50)-y_pad)
P_90 = (1530,(ymax-50)-y_pad)
AllIN = (1710,(ymax-50)-y_pad)
TEST = (221,912)
class Colors:
percent_button = (100, 65, 165)
background = (26, 26, 26)
left_button = (176, 68, 68)
right_button = (52, 158, 255)
我希望这对遇到同样问题的人有所帮助,这对我来说是一个令人沮丧的问题。
【问题讨论】:
-
Python 绝对不会将变量的名称解释为其值。真的很不清楚您的问题是什么,以及您要完成什么。
-
Cord.percent意思是:找到名称Cord引用的对象,然后从该对象中检索属性percent。您有一个名为Cord的对象(它引用了一个类),但该对象没有这样的属性。 -
请参阅How do I test one variable against multiple values? 进行预过滤器测试;你的表达不做你认为它做的事。
-
我怀疑您正在寻找动态属性访问;
getattr(Cord, percent),但您的问题非常不清楚。 -
Cord是什么,Cord.percent你想做什么?
标签: python python-2.7 parameter-passing method-call