Python Class has no attribute

Python Class has no attribute

Gold is not an attribute on the Item class, no. It is a subclass, and a global name in its own right. You can import it from your items module:

>>> from items import Gold
>>> Gold
<class items.Gold>

You cannot create an instance of it, because used the wrong name for the Item.__init__ method:

>>> from items import Item
>>> Item.__init__
<slot wrapper __init__ of object objects>
>>> Item.__init___
<function Item.__init___ at 0x1067be510>
>>> Item(a, b, 4)
Traceback (most recent call last):
  File <stdin>, line 1, in <module>
TypeError: object() takes no parameters

Note that the method you created has three underscores in the name. If you fix that:

class Item():
    def __init__(self, name, desc, val):
        # ^   ^ 2 underscores on both sides
        self.name   = name
        self.desc   = desc
        self.val    = val

you can create instances of the Gold() class:

>>> Gold()
<items.Gold object at 0x1067cfb00>
>>> gold = Gold()
>>> print(gold.print_info())
Gold
==========
Golden coin.

Value: 5

Now, if you really wanted to create attributes on the Item class, youll have to add those after you created the class:

class Item():
    def __init___(self, name, desc, val):
        self.name   = name
        self.desc   = desc
        self.val    = val

    def print_info(self):
        return {}n==========n{}nnValue: {}.format(self.name, self.desc, self.val)

Item.gold = Item(Gold, Golden coin., 5)

You dont need to create subclasses for that. You could use the enum module here though:

from enum import Enum

class Item(Enum):
    Gold = Golden coin., 5
    Silver = Silver coin., 1

    def __init__(self, desc, val):
        self.desc = desc
        self.val = val

    def print_info(self):
        return {}n==========n{}nnValue: {}.format(self.name, self.desc, self.val)

Here Gold is an attribute of Item:

>>> Item
<enum Item>
>>> Item.Gold
<Item.Gold: (Golden coin., 5)>
>>> print(Item.Gold.print_info())
Gold
==========
Golden coin.

Value: 5
>>> Item.Silver
<Item.Silver: (Silver coin., 1)>

Heres what youre doing wrong:

  • Gold is a subclass of Item, not an attribute of it. Your error is popping up when you try to do Item.Gold. Gold is accessed entirely separately.
  • You need to instantiate your classes into objects. Once you instantiate an object, you can call your methods on it and access its attributes. Each object stores methods and attributes independently, so one gold coin can have a different name, description, value, or even print its info differently.
  • When trying to access a parent class from within a subclass, you just reference the class name directly rather than using super().
  • You have an extra underscore in your Item classs __init__()

So with that in mind, your new main.py should look like this:

from items import Gold

mygold = Gold() # This is where we instantiate Gold into an object
print(mygold.print_info()) # We call the method on the object itself

And your items.py will look like this:

class Item():
    def __init__(self, name, desc, val):
        self.name   = name
        self.desc   = desc
        self.val    = val

    def print_info(self):
        return {}n==========n{}nnValue: {}.format(self.name, self.desc, self.val)

class Gold(Item):
    def __init__(self):
        Item.__init__(name = Gold, desc = Golden coin., val = str(5))

Python Class has no attribute

You can call functions that are in Item from the Gold class but not the other way around. So your main should be:

from items import Gold
print(Gold.print_info)

Please note that if you dont end functions with (), then youll just get the string representation of the function. But if you do that in your current code it wouldnt work unless you create the object first and then call print_info().

Leave a Reply

Your email address will not be published.