Prev: Cross-platform module that creates directory object with allfile attributes
Next: death of newsgroups (Microsoft closing their newsgroups)
From: Roald de Vries on 14 Jul 2010 04:32 On Jul 14, 2010, at 3:53 AM, Steven D'Aprano wrote: > On Tue, 13 Jul 2010 19:26:34 +0200, Roald de Vries wrote: > >> Hi all, >> >> I have two objects that should both be able to alter a shared >> float. So >> i need something like a mutable float object, or a float reference >> object. Does anybody know if something like that exists? I know >> it's not >> hard to build, but I have a feeling that there should be a standard >> solution to it. > > One standard solution would be to wrap the float in a class as an > attribute. I think the nicest solution is to do this with a wrapping descriptor class. Thanks for your input! Cheers, Roald
From: Christian Heimes on 14 Jul 2010 05:37 > I know, I just wondered if there is a *standard* solution. Yeah, you have to reset your brain and switch to Python mode. *scnr* Seriously, your inquiry sounds like you are trying to code C in Python. I'm using Python for more than seven years and I've never felt the need for a mutable float ref or something similar. Well, except for the first year when my brain was still in C pointer mode. :)
From: Tino Wildenhain on 14 Jul 2010 16:22 Am 13.07.2010 19:26, schrieb Roald de Vries: > Hi all, > > I have two objects that should both be able to alter a shared float. > So i need something like a mutable float object, or a float reference > object. Does anybody know if something like that exists? I know it's > not hard to build, but I have a feeling that there should be a > standard solution to it. Nice question to reflect our understanding of the basics of this language. From what I know, we have the terms: - reference - name (pointer is not really used beside from indices into lists or such) I see it this way: you have named and unnamed references to objects. This means you can bind any number of names to an object as well as reference the object in a list, tupe or set: >>> (1,2,3) # unnamed references to int objects 1,2 and 3 >>> a=(1,2,3)[1] # binds a to tuple element #1 (int 2 object) >>> b=a # now a and b are bound to the same int 2 object So back to your question: >>> a=b=1.0 # binds the names a and b to the same float object 1.0 >>> a,b (1.0, 1.0) now, there is no way to alter the float object since it is immutable (as there are str, unicode, int, long, tuple...) >>> a+=0.5 >>> a,b (1.5, 1.0) now you see even the "in place" addition creates a new object and binds the name to it. What you'd need to do would probably wrapping the float value into a class where you can simulate a mutable float by providing a constant name or reference for it within the class. For example: >>> a=b=[1.0] >>> a[0]+=0.5 >>> a,b ([1.5], [1.5]) HTH Tino
From: John Nagle on 15 Jul 2010 03:14 On 7/14/2010 12:56 AM, Roald de Vries wrote: > I know, I just wondered if there is a *standard* solution. Yes, there is. class floatref(object) : def __init__(self, initialval) : self.__dict__["_val"] = initialval def __getattr__(self, attrname) : if attrname == 'val' : return(self._val) else : raise AttributeError(attrname) def __setattr__(self, attrname, val) : if attrname == 'val' : self.__dict__["_val"] = val else : raise AttributeError(attrname) x = floatref(1.0) y = x print(x.val) x.val = 10.0 print(x.val) print(y.val) Are you happy now? John Nagle
From: Tino Wildenhain on 15 Jul 2010 05:48
Hi John, Am 15.07.2010 09:14, schrieb John Nagle: > On 7/14/2010 12:56 AM, Roald de Vries wrote: >> I know, I just wondered if there is a *standard* solution. > > Yes, there is. > > class floatref(object) : > def __init__(self, initialval) : > self.__dict__["_val"] = initialval > def __getattr__(self, attrname) : > if attrname == 'val' : > return(self._val) > else : > raise AttributeError(attrname) > def __setattr__(self, attrname, val) : > if attrname == 'val' : > self.__dict__["_val"] = val > else : > raise AttributeError(attrname) Uhm. How would that be different to: class floatref(object): def __init__(self,val): self.val=val ? > > x = floatref(1.0) > y = x > > print(x.val) > x.val = 10.0 > print(x.val) > print(y.val) Regards Tino |