當我嘗試設置從str
繼承的類的參數值時會引發錯誤。只有當我嘗試使用myClass(arg = 'test')
訪問參數時纔會出現錯誤。錯誤是:設置從int或float或str繼承的類中參數的值
TypeError: 'arg' is an invalid keyword argument for this function
這個問題在本例所示:
class A(str):
def __init__(self, arg):
pass
A("test") # Works well
A(arg = "test") # Fails
只有最後一行引發錯誤。前一行運行良好。 問題與從int
或float
繼承的類相同。
UPDATE(溶液):
我發現這些鏈接的解決方案:
- Adding optional parameters to the constructors of multiply-inheriting subclasses of built-in types?
- inheritance from str or int
的解決方案是:
class A(str):
def __new__(cls, *args, **kwargs):
return str.__new__(cls)
def __init__(self, arg01):
print(arg01)
A(arg01= "test")
我不確切知道爲什麼這樣做,我會對此進行調查。如果有人有清楚的解釋,我很感興趣,我提前感謝他。
UPDATE(我的解釋):
我不知道所有的東西,我會說,但是這是我的理解。
想象一下沒有遺傳的類'myClass'
。 當我這樣做myInstance = myClass()
,這裏發生了什麼:
方法myClass.__new__
被執行。此方法將創建對象myInstance
。 __new__
是真正的構造函數(__init__
不是構造函數!)。在僞代碼,__new__
是這個樣子:
def __new__ (cls, *args, **kwargs):
myInstance = # Do some stuff to create myInstance, an object of the type passed as argument (cls).
# Add the method __init__ to myInstance.
# Call __init__ and pass to it the list 'args' and the dictionary 'kwargs' (both come from arguments of __new__). Pass to it also the object itself (self) :
obj.__init__(self, args, kwargs) :
# Do some stuff.
的情況有點不同的,當我們使用不變類型(STR,整型,浮點,數組)。在之前的僞代碼中,我寫了def __new__(cls, *args, **kwargs)
。對於不可變類型,方法__new__
的僞代碼更像此def __new__(cls, anUniqueValue)
。我沒有真正理解爲什麼immutableTypes.__new__
的行爲是不同的,但事實如此。你可以看到它在本例:
class foo():
def __init__(self):
pass
foo.__new__(foo, arg = 1)
# That works because the method __new__ look like this : def __new__(*args, **kargs).
str.__new__(str, arg = 1)
# That fails because we are trying to pass the attribute 'arg' to a method which look like this : def __new__(anUniqueValue).
從這裏,我們可以理解,爲什麼前面提供的解決方案工作。我們所做的就是編輯一個不可變類型的方法__new__
,就像一個可變類型一樣工作。
def __new__(cls, *args, **kwargs):
return str.__new__(cls)
這兩條線轉換def __new__ (cls, anUniqueValue)
到def __new__ (cls, *args, **kwargs)
我希望我的解釋是幾乎清晰,沒有這麼多的失誤。如果你講法語,你可以在這個鏈接上了解更多:http://sametmax.com/la-difference-entre-new-et-init-en-python/
可能與http://stackoverflow.com/questions/3748635/adding-optional-parameters-to-the-constructors-of-multiply-inheriting-subclasses有關,雖然我不確定Alex Martelli的意思是'同樣任意的超級「。 – timgeb
@摩根,解釋是在相關的問題中,也可能有一個*她*,可以爲您提供一個清晰的解釋;) –
@摩根毫不猶豫地回答你自己的問題,當你發現究竟是怎麼回事上。我自己並沒有完全理解相關問題的解釋。 – timgeb