1. 程式人生 > >python中 報錯xxx takes 1 positional argument but 2 were given問題解決

python中 報錯xxx takes 1 positional argument but 2 were given問題解決

python中的類預設第一個形參delf必傳,呼叫時傳入的實引數對應的是delf後面的形參:

class Cat:
    ## 屬性

    ## 方法
    def eat(delf, val):
        print('---The cat is eating ' + str(val))
    ## 第一個引數delf必傳
    def dring(delf, drinking):
        print('---The cat is drinking ' + str(drinking))

smcat = Cat()
## food對應val,milk對應drinking
smcat.eat('food')
smcat.dring('milk')