Найти - Пользователи
Полная версия: super()
Начало » Python для новичков » super()
1
Soteric
Проясните, пожалуйста. Есть класс ConnectionObject, унаследованный от класса Connection.
class ConnectionObject(Connection):
def authenticateWithPassword(self):
super(ConnectionObject, self).authenticateWithPassword(self.username, self.password)

def __init__(self, username, password):
super(ConnectionObject, self).__init__()
self.username = username
self.password = password
У родительского класса Connection есть метод authenticateWithPassword(self, username, password). Следующий код
con = ConnectionObject(username, password)
con.authenticateWithPassword()
дает следующую ошибку
File “D:\developing\ide\projects\netbeans\jython\SshTest\src\sshhelper.py”, line 45, in get_connection
con.authenticateWithPassword()
File “D:\developing\ide\projects\netbeans\jython\SshTest\src\sshhelper.py”, line 16, in authenticateWithPassword
super(ConnectionObject, self).authenticateWithPassword(self.username, self.password)
TypeError: authenticateWithPassword() takes exactly 1 argument (3 given)
Т.е. такое впечатление, что ConnectionObject вызовом super(ConnectionObject, self).authenticateWithPassword(self.username, self.password) обращается к самому себе. Если заменить эту строку на
Connection.authenticateWithPassword(self, self.username, self.password)
то работает. Почему не работает в первом случае и как правильнее?
Soteric
Кажется нашел.

http://fuhm.net/super-harmful/

Это ужасно :)
Андрей Светлов
Это ужасно - при наследовании перекрывать виртуальные функции другими, с измененной сигнатурой
Soteric
Да, пожалуй :) Заведу другой метод.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB