2

I'm trying to save an class object into dictionary

I've something like this

class Thing:

    def __init__(self, symbol, age, pos):

        worldMap = {}

        self.symbol = symbol
        self.age = age
        self.pos = pos


t1 = Thing('c', 2, '')
t2 = ..

t1.worldMap

When I try to add t1 to the worldMap it doesn't work. What am I missing here?

0

3 Answers 3

1

save an class object into dictionary, so i can use it in an array.

This doesn't make any sense.

Any way, in order to save an object as a key in a dictionary you will need to override the __hash__ method. It should return the hash of its members (those that will be unique to each instance of the class).

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

1 Comment

Yeah, you are right, i wanted to save an object as a key in a dictionary.
1

The worldmap is an attribute of your instance t1, adding the instance to itself is not what you want I guess.

You either have to create a global variable world map holding all the Class-Instances or make it static. You should rather go for the first approach though, else your Thing-Class will hold the reference to itself again.

Comments

1

Didn't quite get what you are trying to achieve, but it looks like the problem is you just using new dict every time. If that is the case, then you just need to save the dict somewhere:

class Thing:
    def __init__(self, symbol, *args, **kwargs):
        # get existing dict, or create it for the first run
        worldMap = getattr(self.__class__, 'worldMap', {})
        worldMap.update(symbol, self)

        # save the dict to use it later
        self.__class__.worldMap = worldMap

t1 = Thing('c', 1, 2)
assert(t1.worldMap['c'] == t1)

5 Comments

i think i put worldMap in wrong place. if i say class Thing: worldMap = {} def __init__(self, symbol, age, pos, number): Does it make like this more sense? This is how someone showed my how to save object as an key in a dictionary, but it doesnt work when i try it.
Moving worldMap out of __init__ within the same class will not make much difference. You may move it out from class like this: worldMap={}; class MyClass: __init__(...)
I tried it out, and i think i get it now, the only problem left is, that it says ''Thing' object is not iterable'
Ok, I guess that you are trying to use t1 as a key or as a dict itself (which does not rise that particular error, I am just guessing). That's not going to work. You didn't provide an example code of what you actually do with the worldMap, so I assume it's something like worldMap[t1] = "some other stuff". In order to do so you need your Thing to be hashable.
Yeah, i think there is a problem in my logic, and i'm new to programing, still making many mistakes. I made a matrix, i wanted then to create a Thing that can move in the matrix. That 'Thing' object should have a symbol, age and position. But i will just read more about classes and objects, don't understand it fully yet. Thank you so much for your help and time, i really appreciate it :)

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.