2

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
Community
  • 1
  • 1
Cupitor
  • 11,007
  • 19
  • 65
  • 91
  • 2
    If you want to map objects to anything, use a hash table. For that, you need hash and equality methods. – Fred Foo Jun 21 '13 at 20:09
  • @larsmans Where can I read and learn about it? – Cupitor Jun 21 '13 at 20:10
  • @Naji If you want `f(a)` to be 25, are you declaring `a=MyObject(25)`? or will `a` still be declared as `a=MyObject(12)`? – James Jun 21 '13 at 20:16
  • @Imagine no! The objects has a name. But I also want a function like f which assigns 25 to a and 36 to b which has been asnwered by Nik. Thanks anyway. – Cupitor Jun 22 '13 at 15:44

6 Answers6

6

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
Nik
  • 5,515
  • 14
  • 49
  • 75
1

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.

dan
  • 982
  • 8
  • 24
1

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
Niklas R
  • 16,299
  • 28
  • 108
  • 203
0

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
jh314
  • 27,144
  • 16
  • 62
  • 82
0

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
James
  • 2,635
  • 5
  • 23
  • 30
0

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

Mazurov
  • 78
  • 5