user5630070
user5630070

Reputation: 15

Doubts on objects in python code

i've copied a python code from a guide:

class Carta:

    ListaSemi=["Fiori","Quadri","Cuori","Picche"]
    ListaRanghi=["impossibile","Asso","2","3","4","5","6",\
                "7","8","9","10","Jack","Regina","Re"]

    def __init__(self, Seme=0, Rango=0):
        self.Seme=Seme
        self.Rango=Rango 

    def __str__(self):
        return (self.ListaRanghi[self.Rango] + " di " + self.ListaSemi[self.Seme])

    def __cmp__(self, Altro):
        #controlla il seme
        if self.Seme > Altro.Seme: return 1
        if self.Seme < Altro.Seme: return -1

        #se i semi sono uguali controlla il rango
        if self.Rango > Altro.Rango: return 1
        if self.Rango < Altro.Rango: return -1

        return 0

when i call from shell:

>>> Carta1=Carta(1,11)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

I'm using python version 2.7. what's wrong?? thanks

Upvotes: 0

Views: 60

Answers (1)

Byte Commander
Byte Commander

Reputation: 6776

I assume that the snippet above is saved as Carta.py and that you ran in your interactive shell:

>>> import Carta
>>> Carta1=Carta(1,11)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

This way, you try to call/instantiate the module instead of the class inside it. You have basically two options to fix this, changing either the import or the constructor call:

  • >>> from Carta import Carta
    >>> Carta1=Carta(1,11)
    
  • >>> import Carta
    >>> Carta1=Carta.Carta(1,11)
    

If you rename either the module file or the class so that you can distinguish them better, it becomes clear.

Upvotes: 3

Related Questions