python - 无参数实例化
问题描述
所以我试图使用另一个类的方法,但我必须使用方法从类中添加所有参数。有没有办法解决?
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "aaaa")
label.pack(pady = 10, padx = 10)
A = managers()
method = getattr(A, printAll) ## "printAll" is the method im trying to get
addemployee = tk.Button(self, text = "Start", command = method)
addemployee.pack(pady = 10, padx = 10)
class managers(workers):
def __init__(self, firstname, surname, age, postcode, wage, Employees = None):
super().__init__(firstname, surname, age, postcode, wage)
def printAll(self): #### the method i want to use
for emp in self.employees:
print(emp.Fullname(), "-", emp.Age(), "-", self.Postcode(), "-", self.Wage())
解决方案
您可以使用默认值,因此您不必传递所有内容。
class managers(workers):
def __init__(self, firstname="fname", surname="sname", age="196", postcode="pc", wage="1$", Employees = None):
如this SO answer中所建议的,另一个允许您完全自由的选项是使用 args 和关键字 args 传递参数def __init__(self, *args, **kwargs):
推荐阅读
- java - XWPFDocument - 替换文本不起作用
- algorithm - Kotlin 默认使用哪种排序方式?
- java - 硒中的WebElements与WebElement的区别
- php - PHP-在数据库表中链接以仅显示一个表行
- apache-camel - 骆驼 XPATH 变量到 XQuery element() 转换问题
- machine-learning - pytorch 中内核大小和填充的默认值是多少?
- c# - 检查 html 是否包含标签和空格
- rxjs - 如何取消订阅所有观看由“fromEvent”制作的可观察对象的观察者?
- angular - Angular Universal + PM2:不断退出并重新启动
- chatbot - 导出应用程序时,我的实体被删除