Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更有效地调用类的方法_Python_Class_Oop_Methods_Tk - Fatal编程技术网

Python 更有效地调用类的方法

Python 更有效地调用类的方法,python,class,oop,methods,tk,Python,Class,Oop,Methods,Tk,我的代码没有任何问题,我只是想看看是否有一个更短的方法 def taskcreator(first,second,third): first.needs() second.needs() third.needs() 如果我能做点像 def taskcreator(first,second,third): (first,second,third).needs() 因为我将创建更多的对象,我喜欢尽量减少代码数量,提高代码效率。 提前谢谢你的帮助。 这是全部代码 i

我的代码没有任何问题,我只是想看看是否有一个更短的方法

def taskcreator(first,second,third):
    first.needs()
    second.needs()
    third.needs()
如果我能做点像

def taskcreator(first,second,third):
    (first,second,third).needs()
因为我将创建更多的对象,我喜欢尽量减少代码数量,提高代码效率。 提前谢谢你的帮助。 这是全部代码

import tkinter as tk
from tkinter import ttk

screen = tk.Tk()

class Crop:
    def __init__(self,sizename,sizevar,onvar,xvalue,yvalue):
        sizevar = tk.StringVar()
        self.sizename = sizename
        self.sizevar = sizevar
        self.onvar = onvar
        self.offvalue = 'no'
        self.cb = tk.Checkbutton(screen, text=self.sizename, variable=self.sizevar, onvalue=self.onvar, offvalue=self.offvalue)
        self.cb.deselect()
        self.cb.place(x=xvalue, y=yvalue)

    def needs(self): 
        value = self.sizevar.get()
        print(value)
        if value != 'no':
            print(self.sizename + ' has been checked')
        else:
            print(self.sizename + ' has not be checked')


def main():

    labell = tk.Label(screen, text='Enter Shoe Link').place(x=10, y=20)
    first = Crop('UK 2.5', 'twohalf', 'twohalfon', 10, 110)
    second = Crop('UK 3', 'three', 'threeon', 200, 110)
    third = Crop('UK 3.5', 'threehalf', 'threehalfon', 10, 150)
    fourth = Crop('UK 4', 'four', 'fouron', 200, 150)
    fith = Crop('UK 4.5', 'fourhalf', 'fourhalfon', 10, 190)
    sixth = Crop('UK 5', 'five', 'fiveon', 200, 190)
    seventh = Crop('UK 5.5', 'fivehalf', 'fivehalfon', 10, 230)
    eigth = Crop('UK 6', 'sixth', 'sixthon', 200, 230)
    ninth = Crop('UK 6.5', 'sixthhalf', 'sixthhalfon', 10, 270)
    tenth = Crop('UK 7', 'seven', 'sevenon', 200, 270)
    newtaskbutton = tk.Button(screen, text='Start', command=lambda: taskcreator(first,second,third))
    newtaskbutton.place(x=30,y=10)

def taskcreator(first,second,third):
    first.needs()
    second.needs()
    third.needs()
main()
screen.mainloop()

您可以使用
getattr
*args
来实现这一点:

def taskcreator(*args):
    for arg in args:
        getattr(arg, 'needs')()
*args
获取传递到函数中的所有参数,并将它们存储为名为
args
元组


<> >代码> GATTARC 按名称获取一个对象的属性——在这种情况下,<代码>需要< <代码>属性>每个代码> ARG

伟大-如果解决了您的问题,请考虑接受这个答案:您不需要使用<代码> GATTARC ;code>arg.needs()
将起作用。是的-我添加了
getattr
,因为我不确定只有该方法是动态的-但是如果它只是
needs
那么
getattr
是多余的。