Python报错:module.__init__()最多接受2个参数,求排查解决(附三类代码)
module.__init__() takes at most 2 arguments Error in Your Python Linked List Code Let's break down exactly what's causing this error and how to fix it, based on the code you shared for Nodo.py, Lista.py, and ListaEnlazada.py.
First off, this error almost always means you're trying to instantiate a Python module (the .py file itself) as if it were a class. Modules have a built-in __init__ method that only accepts up to 2 arguments, which clashes with how you're trying to use it like your custom classes.
Most Likely Scenarios That Trigger This Error
1. Incorrect Import Syntax
Chances are, in ListaEnlazada.py, you're importing your modules like this:
import Nodo import Lista
Then trying to create instances like this:
new_node = Nodo() # ❌ Wrong! Nodo here is the module, not the class inside it my_list = Lista() # ❌ Same issue: Lista refers to the .py file, not the class
When you call Nodo() or Lista() this way, you're calling the module's constructor, not your custom class's constructor—hence the argument error.
2. Accidentally Inheriting From a Module (Not a Class)
If your ListaEnlazada class is supposed to inherit from Lista, you might have written something like this:
import Lista class ListaEnlazada(Lista): # ❌ Inheriting from the module, not the Lista class def __init__(self): super().__init__()
Inheriting from a module instead of the class inside it will throw this error when the parent constructor runs.
Step-by-Step Fixes
Fix Your Import Statements
You need to explicitly import the classes from their respective modules, not just the modules themselves. Here are two ways to do this:
Option 1: Import the Class Directly
from Nodo import Nodo from Lista import Lista
Now you can instantiate your classes normally:
new_node = Nodo("test data") my_list = Lista()
Option 2: Reference the Class via the Module
If you prefer keeping the module name as a prefix, make sure to specify the class:
import Nodo import Lista new_node = Nodo.Nodo("test data") my_list = Lista.Lista()
Correct Inheritance (If Applicable)
If ListaEnlazada is meant to extend Lista, make sure you're inheriting from the actual class:
from Lista import Lista class ListaEnlazada(Lista): def __init__(self): super().__init__() # Now this calls the Lista class's __init__, not the module's self._head = None # Add your linked list-specific attributes here def add(self, data): new_node = Nodo(data) new_node.setSiguiente(self._head) self._head = new_node self._tamanio += 1
Quick Validation Checks
- Double-check that your
Lista.pyfile has a completeListaclass definition (your code snippet cuts off atdef elemento(self,po...—make sure there are no syntax errors that prevent the class from being recognized). - Ensure all three
.pyfiles are in the same directory, or that your Python interpreter can find them in its path (this avoids silent import failures that might lead to unexpected module/class mix-ups).
Here's a working example of ListaEnlazada.py using the fixes above:
from Nodo import Nodo from Lista import Lista class ListaEnlazada(Lista): def __init__(self): super().__init__() self._cabeza = None def agregar(self, dato): nuevo_nodo = Nodo(dato) nuevo_nodo.setSiguiente(self._cabeza) self._cabeza = nuevo_nodo self._tamanio += 1 # Test it out mi_lista = ListaEnlazada() mi_lista.agregar("Primer elemento") print(f"Tamaño de la lista: {mi_lista._tamanio}") # Output: Tamaño de la lista: 1
That should resolve the module.__init__() argument error for you.
内容的提问来源于stack exchange,提问作者mpop15

