0

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

2
  • 4
    You didn't show what you typed to import this; looks like you have imported the module, not the class inside it. Commented Dec 2, 2015 at 11:48
  • I copied your code and is working fine. Save the code in file and then try running your file instead of python shell and import that as module. Commented Dec 2, 2015 at 11:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, see the naming conventions from PEP008. So, in this case it would suggest a module name of carta.py and the class as in the original code - Carta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.