简体   繁体   中英

Class has no attribute, even though set

I've been making a dungeons and dragons themed game in python and have been using classes for the player and maps.

But when trying to fetch an array from the map class,it tells me there is no attribute named by this, but it has definitely been set.

Heres the map class

class Map_1:

    def __init__ (self):
        self.Title = "Default"
        self.Spawn = [4, 2]
        self.map = [...]

I then go on to try and use this

Map = Map_1
current_pos = Map.Spawn

Now usually this would be embedded in another Class, but i tried seeing if it was classes clashing by taking it out and running it without a class and i still get an error. This is my error

class Map_1 has no attribute 'Spawn'

Your class has no such attribute no. Only instances of the class would have that attribute, but you never create one.

Create an instance by calling the class:

Map = Map_1()  # call the class
current_pos = Map.Spawn

To elaborate on Martijn's answer, you are missing the '()' after 'Map_1'. When you say

Map = Map_1

that needs to be:

Map = Map_1()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM