object references

DrConti daconti.mail at gmail.com
Sun Mar 26 01:13:28 EST 2006


Felipe Almeida Lessa schrieb:

> Em Sáb, 2006-03-25 às 21:33 -0800, DrConti escreveu:
> [snip]
> > There was also a suggestion to write a real problem where referencing
> > is really needed.
> > I have one...:
> [snap]
>
> There are loads of discussions about the code you wrote... but... isn't
> bad practice to put the same data in two places? Or am I missing
> something?
>
> Cheers,
>
> --
> Felipe.
Hi Felipe, surely it's bad practice to put the same data in two places.
However I don't want to put the data in the identifier list, but just
the reference to the attributes.

My general problem is to find a way to define subsets of instance
attributes (for example the identifier),  so that at later time I can
just iterate over the subset.
In the meantime I found a 90% solution to the problem through lambdas..
See now the code below: maybe you'll understand my point better.
Thanks and Regs,
Davide

class ObjectClass:
	""" Test primary Key assignment
	"""
	def alias(self,key): return lambda: self.__dict__[key]
	def __init__(self):
		self.Identifier=[]

	def getPK(self):
		return [ GetPKValue() for GetPKValue in self.Identifier ]

if __name__ == "__main__":
	ObjectClassInstantiated=ObjectClass()

	ObjectClassInstantiated.AnAttribute='First PK Elem'
	ObjectClassInstantiated.AnotherOne='Second PK Elem'
	ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.alias('AnAttribute'))
	ObjectClassInstantiated.Identifier.append(ObjectClassInstantiated.alias('AnotherOne'))
	print ObjectClassInstantiated.getPK()
	ObjectClassInstantiated.AnAttribute='First PK Elem Changed'
	print ObjectClassInstantiated.getPK()

>./test.py
['First PK Elem', 'Second PK Elem']
['First PK Elem Changed', 'Second PK Elem']
   --> correct now!




More information about the Python-list mailing list