0

I'm a newbie and I'm having a really strange (like in the title, for me) problem. This is the code:

    from random import shuffle
    class Carta:
            listaSemi=[" ","Bastoni","Spade","Coppe","Denari"]
            listaValori=[" ","Asso","Due","Tre","Quattro",
    "Cinque","Sei","Sette","Otto","Nove","Dieci"]   
            def __init__(self,seme,valore):
                    self.seme=seme
                    self.valore=valore
            def __str__(self):
                    s1=self.listaValori[self.valore]
                    s2=self.listaSemi[self.seme]        
                    return " ".join((s1,"di",s2))
    class Mazzo:
            def __init__(self):
                    self.Carte=[]
            def Crea(self):
                    for seme in range(1,5):
                            for valore in range(1,11):
                                    self.Carte.append(Carta(seme, valore))
            def Mescola(self):
                    shuffle(self.Carte)

When i do:

M=Mazzo
Mazzo.Crea(M)

I get:

 Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    Mazzo.Crea(M)
  File "/home/administrator/Scrivania/Script/Carte.py", line 19, in Crea
    self.Carte.append(Carta(seme, valore))
  AttributeError: type object 'Mazzo' has no attribute 'Carte'

Thanks in advance for your help!

4
  • 1
    Did you want M to be an instance of Mazzo? Then do M=Mazzo() with parenthesis. Otherwise M is just a reference to the class Mazzo and Mazzo.Crea(M) just means Mazzo.Crea(Mazzo). Once M is an instance of Mazzo, Mazzo.Crea(M) can also be written M.Crea(). Commented May 5, 2015 at 15:03
  • shouldn't it be M.Crea() and M = Mazzo() ? Also your Crea() method takes no parameter, so Mazzo.Crea(M) is syntactically incorrect. Commented May 5, 2015 at 15:03
  • Thanks for your time, guys. I'm really sorry for my ignorance. Commented May 5, 2015 at 15:08
  • Nobody was born knowing python, we all struggled to acquire the knowledge we have. No need to apologize! Commented May 6, 2015 at 2:17

4 Answers 4

3

You are initilizing your class incorrectly. Try doing this:

m = Mazzo()
m.Crea()

So a quick explanation as to why. First of all, the first line initializes an object of type Mazzo and sets in to m (please note the '()' you need that for all methods, initialization or otherwise.) Since its only perimeter is self it can be left blank.

Next we want to call the Crea function, we do this by calling the object we just created and NOT the class itself.

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

Comments

2

You are assigning type to M instead of creating a new object. Probably you want to do this:

m=Mazzo()
m.Crea()

Comments

2

The problem is in how you are instantiating the class Mazzo

>>>M=Mazo

What is really assigned to M is the class object and not a Mazzo object:

>>>type(M)
<class 'type'>

Your next line is even more problematic:

>>> Mazzo.Crea(M)

That asks the Mazzo class to execute the Crea method, with a Mazzo class object as the argument. And Crea doesn't take any other arguments, let alone a class object. I suspect you may have some more reading to do about the self method argument in python.

The usual way of instantiating a new object of a given class is something like:

>>> class MyClass(object):
        def __init__(self,someValue):
            self.someValue = someValue
        def aMethod(self)
            print(self.someValue)

>>> newObject = MyClass(42)
>>> newObject.aMethod()
42

Good luck.

Comments

0

When you did

M=Mazzo Mazzo.Crea(M)

What you did is Mazzo.Crea(Mazzo).

First off, M=Mazzo should be M=Mazzo(). When you call just plain Mazzo, it doesn't trigger __init__, although Mazzo() does.

FYI: calling Mazzo.Crea(Mazzo) will raise a TypeError, as in

TypeError: Crea takes exactly 1 argument (2 Given)

Comments

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.