Ho un codice Python che copre diversi file che ho impacchettato per comodità, finendo con i seguenti 3 file nella directory my_package:autoreload e package che causano TypeError: super (type, obj): obj deve essere un'istanza o sottotipo di tipo
__init__.py
contenuti:
from file1 import *
from file2 import *
contenuti file1.py:
class Base(object):
pass
file2.p Contenuti y:
from file1 import Base
class Derived(Base):
def __init__(self):
return super(Derived, self).__init__()
ho quindi eseguire in IPython:
>>>%autoreload 2
>>>import my_package
>>>t = my_package.Derived()
Fin qui tutto bene. Ma poi apporto le modifiche a file2.py, ad esempio aggiungendo un attributo dummy. Ora, quando eseguo:
>>>t = my_package.Derived()
>>> 2 class Derived(Base):
>>> 3 def __init__(self):
>>>----> 4 return super(Derived, self).__init__()
>>> 5
>>> 6 dumm = 'asdf'
>>>
>>>TypeError: super(type, obj): obj must be an instance or subtype of type
Questo non va via fino a che non riavviare la console IPython. Perché il caricamento automatico non si occupa correttamente di questo? Tutto funziona se metto Base e Derivato in un singolo file di modulo piuttosto che in un pacchetto.
Questo ha senso. Ho provato a usare "da file2 import Derived" piuttosto che a *, ma purtroppo non è stato risolto. È strano che autoreload non riesca a capire quel pacchetto. Dovrebbe essere aggiornato. – Leo
@Leo L'errore sembra verificarsi quando si utilizza 'da A.B import C' se B contiene un file' __init __. Py'. Questo mi infastidisce troppo. – Edityouprofile