Cupitor
Cupitor

Reputation: 11637

How to map objects to integers in python?

I want to have a map from objects of a class called MyObject to integers. I read some stuff here, but didn't understand anything and that doesn't seem to be quite the thing I want. Hash function can only be used for collision. What I want to do is to retrieve the value(integer) by giving the object as argument. Basically what I look for is a function(in mathematical terms) from MyObject to integers.

So suppose this is the definition of my class:

class MyObject:
    def __init__(self,number):
        self.name = name
        self.marked=False

So for example

a=MyObject("object 1")
b=MyObject("object 2")

Now I want some mapping like f, that I could assign 25 to a and 36 to b. And be able to get:

f(a)=25
f(b)=36

Upvotes: 2

Views: 2725

Answers (6)

Mazurov
Mazurov

Reputation: 78

You could not use assign operator in this way: "my_object_instance=number", but you can overload some other operator, for example, "<<":

class MyObject:
    def __init__(self,name):
        self.name = name
    def __lshift__(self, num):
        self.name = str(num)

def f(a):
    return a

a = MyObject(100)
print(a.name)

f(a) << 10
print(a.name)

http://ideone.com/whn20i

Upvotes: 0

Nik
Nik

Reputation: 5745

I don't completely understand yor question. My interpretation is that you want to use objects to index some integers. If that is the intent, you can use a dict.

class MyClass:
    # your code here

c1 = MyClass()
c2 = MyClass()

D = dict({c1:1,c2:2})

D[c1] # will return 1
D[c2] # will return 2

Upvotes: 5

dan
dan

Reputation: 1002

You seem to be talking about the id() function. From the manual: Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Upvotes: 1

jh314
jh314

Reputation: 27792

How about this:

id(myObj)

This should return the object id (as an integer)

From your edit, a dictionary should do what you want:

# a and b are instances of your object
f = {}
f[a] = 25
f[b] = 36

Then you can access the integers as:

print f[a]
>>> 25

Upvotes: 0

James
James

Reputation: 2795

Is this what you mean?

class MyObject:
    def __init__(self,name):
        self.name = name
        self.number = None

a = MyObject("a")
a.number = 25

f = lambda x : x.number
print f(a) #prints 25

Upvotes: 0

Niklas R
Niklas R

Reputation: 16870

From your question, it looks like you want the objects as keys of a dictionary:

d = {a: 25, b: 36}
assert d[a] == 25

But this wouldn't make much sense since you could store the number directly in the object.

class MyObject:
    def __init__(self, name, number):
        self.name = name
        self.number = number

a = MyObject("a", 25)
b = MyObject("b", 36)

Maybe you need to associate the number with the object instead?

d = {25: a, 36: b}
assert d[25] == a

Upvotes: 1

Related Questions