Object default value
James Stroud
jstroud at mbi.ucla.edu
Tue Sep 20 16:29:37 EDT 2005
I think you want to overload the assignment operator here. I'm not sure that
is allowed in python (I've never seen it done). You can overload the
equality, lt, gt, le, ge operators (==, <, ...) such that
class Thing:
x = 5
def __str__(self):
return str(self.x)
def __eq__(self, other):
if hasattr(other, 'x'):
return self.x == other.x
else:
return self.x == other
py> athing = Thing()
py> athing.x
5
py> bob = athing.x
py> athing == bob # having overlaoded equality for athing
True
py> athing is bob # not the same object
False
But I don't think assignment overloading is allowed in python:
py> athing = Thing()
py> print athing # having overloaded __str__()
5
py> bob = athing
py> type(bob) # will not be an int ==> python language constraint
<type 'instance'>
James
On Tuesday 20 September 2005 13:05, ago wrote:
> The print statement was only for illustrative purposes, when calling
> varx=myobj I need to receive obj.x as opposed to the instance of obj,
> but I also need to call vary=myobj.y. Something like that exists for
> com objects/VB, for instance an excel range object uses value as the
> default attribute, so that you can write
>
> set rng=range(...);
> x=rng
> y=rng.value
> 'x==y
> z=rng.attributeXYZ
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list