I would really appreciate a help to this problem:

Let's say
x = Class()
x.name = 'the_name'
x.value = 10

if you were only give the 'the_name' how do you get the instance ref, x to get x.value

Recommended Answers

All 8 Replies

Well, if you think about it, the problem does not have a unique solution.

x = Class()
x.name = 'the_name'
x.value = 10

y = Class()
y.name = 'the_name'
y.value = 20

So now ... do you want x.value or y.value?

Maybe both?

Jeff

Actually I should have mentioned this but that the name is unique and that no other object will have the same name. Any idea on how I can access it's value or get the object x?

And thanks for the reply!

I would really appreciate a help to this problem:

Let's say
x = Class()
x.name = 'the_name'
x.value = 10

if you were only give the 'the_name' how do you get the instance ref, x to get x.value

You might try keeping a class variable that maps all names to named instances, intercepting any setting of .name via properties:

>>> class Class(object):
	_name2inst = dict()
	@property
	def name(self, value):
		'Name property'
		return self._name
	@name.setter
	def name(self, value):
		self._name = value
		self._name2inst[value] = self
	@name.deleter
	def name(self):
		del self._name2inst[self._name]
		del self._name
	def name2inst(self, name):
		return self._name2inst[name]

	
>>> x = Class()
>>> x.name = 'the_name'
>>> y = Class()
>>> y.name = 'the_other_name'
>>> x == x.name2inst('the_name') == y.name2inst('the_name')
True
>>> y == x.name2inst('the_other_name') == y.name2inst('the_other_name')
True
>>>

You might also see if the problem can be solved by keeping a dictionary of objects, mapping names to Class() instances without individual Python variables for each instance.
-- Paddy.

I don't think I understood the code above well enough but will I get the value(=10) from that?

Is there another way of getting the object, x without doing the above(if possible without creating new classes). The idea is that you are only given the name of the object and from that find a way to get the other stuff. I don't necessary need to know the object but I'd like to find a way to get the value of the object with the name 'the_name'

But thanks for the reply!

I should also indicate that not only will the name be unique but you would also know what class the object is in this case it's class is Ob.

(The object is already created, I just want to find a way to refer to that the object so I can use it to find its name, value and other variables that may have been defined in its creation.)

following on from my earlier post:

>>> x.value = 10
>>> x.weight = 1000
>>> y.value = 20
>>> y.weight = 2000
>>> x.name2inst('the_other_name').value
20
>>> x.name2inst('the_other_name').weight
2000
>>> x.name2inst('the_name').value
10
>>> x.name2inst('the_name').weight
1000
>>>

Earlier I said:

You might also see if the problem can be solved by keeping a dictionary of objects, mapping names to Class() instances without individual Python variables for each instance.

The following shows this, simpler method in action, I would use this in preference if it applies:

>>> class Class2(object):
	pass

>>> inst = {}
>>> inst['the_name'] = Class2()
>>> inst['the_other_name'] = Class2()
>>> inst['the_name'].value = 10
>>> inst['the_name'].weight = 1000
>>> inst['the_other_name'].value = 20
>>> inst['the_other_name'].weight = 2000
>>> inst['the_name'].value
10
>>> inst['the_name'].weight
1000
>>> inst['the_other_name'].value
20
>>> inst['the_other_name'].weight
2000
>>> inst['the_name']
<__main__.Class2 object at 0x012D0490>
>>> inst['the_other_name']
<__main__.Class2 object at 0x012D05B0>
>>>

- Paddy.

If you are just keeping a table of fixed fields that are to be accessed by name, then you might use a dictionary of tuples or namedtuples.

Here is an example using named tuples (Python 2.6), together with several ways of accessing the data once saved:

>>> from collections import namedtuple
>>> Data = namedtuple('Data', 'value, weight')
>>> data = {}
>>> data['the_name'] = Data(10, 1000)
>>> data['the_other_name'] = Data(20, 2000)
>>> data['the_name'].value
10
>>> data['the_name'].weight
1000
>>> data['the_other_name'].value
20
>>> data['the_other_name'].weight
2000
>>> data['the_name']
Data(value=10, weight=1000)
>>> data['the_other_name']
Data(value=20, weight=2000)
>>> data['the_name'][0]
10
>>> data['the_name'][1]
1000
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.