Re: Looking for assignement operator

2006-10-25 Thread Bruno Desthuilliers
Tommi wrote:
 Bruno Desthuilliers wrote:
 
(about Traits)

 How could it help ?
 
 To me they just looked a bit alike:
 
 --- op's example ---
 a = MyInt(10)
 # Here i need to overwrite the assignement operator
 a = 12
 
 --- traits' example ---
 moe = Child()
 # NOTIFICATION in action
 moe.age = 10
 

You do understand the difference between rebinding a name and modifying
a mutable object, do you ?

FWIW, you definitively don't need Traits here - properties are enough.

class Something(object):
  @apply
  def age():
def fget(self):
  return self._age
def fset(self, val):
  self._age = MyInt(val)
return property(**locals())

s = Something()
s.age = 42
type(s.age)

But this is not what the OP was asking for...

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-20 Thread Tommi

Bruno Desthuilliers wrote:

 (please don't top-post - corrected)

(sorry)


 How could it help ?

To me they just looked a bit alike:

--- op's example ---
a = MyInt(10)
# Here i need to overwrite the assignement operator
a = 12

--- traits' example ---
moe = Child()
# NOTIFICATION in action
moe.age = 10

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-18 Thread Tommi
Could the traits package be of help?

http://code.enthought.com/traits/



Alexander Eisenhuth wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

 class MyInt:
   def __init__(self, val):
   assert(isinstance(val, int))
   self._val = val

 a = MyInt(10)

 # Here i need to overwrite the assignement operator
 a = 12
 
 
 Thanks
 Alexander

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-18 Thread Bruno Desthuilliers
Tommi wrote:
(please don't top-post - corrected)
 
 
 Alexander Eisenhuth wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

 class MyInt:
  def __init__(self, val):
  assert(isinstance(val, int))
  self._val = val

 a = MyInt(10)

 # Here i need to overwrite the assignement operator
 a = 12


 Could the traits package be of help?

 http://code.enthought.com/traits/


How could it help ?

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-18 Thread Robert Kern
Bruno Desthuilliers wrote:
 Tommi wrote:
 (please don't top-post - corrected)

 Alexander Eisenhuth wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

 class MyInt:
 def __init__(self, val):
 assert(isinstance(val, int))
 self._val = val

 a = MyInt(10)

 # Here i need to overwrite the assignement operator
 a = 12

 
 Could the traits package be of help?

 http://code.enthought.com/traits/
 
 How could it help ?

It doesn't. (I am an Enthought developer.)

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth.
   -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for assignement operator

2006-10-17 Thread Alexander Eisenhuth
Hello,

is there a assignement operator, that i can overwrite?

class MyInt:
def __init__(self, val):
assert(isinstance(val, int))
self._val = val

a = MyInt(10)

# Here i need to overwrite the assignement operator
a = 12


Thanks
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Simon Brunning
On 10/17/06, Alexander Eisenhuth [EMAIL PROTECTED] wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

Soirry, no, assignment is a statement, not an operator, and can't be overridden.

-- 
Cheers,
Simon B
[EMAIL PROTECTED]
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Fredrik Lundh
Alexander Eisenhuth wrote:

 is there a assignement operator, that i can overwrite?

no.

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Rob Wolfe

Alexander Eisenhuth wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

You can't overwrite assignment operator, but you can
overwrite methods of numeric objects:

http://docs.python.org/ref/numeric-types.html

HTH,
Rob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Laurent Pointal
Alexander Eisenhuth a écrit :
 Hello,
 
 is there a assignement operator, that i can overwrite?

Adding to Simon Brunning reply (assignment is a statement).

 class MyInt:
 def __init__(self, val):
 assert(isinstance(val, int))
 self._val = val
 
 a = MyInt(10)
 
 # Here i need to overwrite the assignement operator
 a = 12

Here you bind the 12 (Python int value) to name 'a', then 'a' has the
int type, not your MyInt (which value has been lost).

You may define a 'set' method, and write:
a = MyInt(10)
a.set(12)

And, if a is a member of another class, you may define an accessor for
that 'a' member in that class, which automatically call your set method
when giving an int value.

b.a = MyInt(10)
b.a = 12--- b.a.set(12)

A+

Laurent.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Steven D'Aprano
On Tue, 17 Oct 2006 15:50:47 +0200, Alexander Eisenhuth wrote:

 Hello,
 
 is there a assignement operator, that i can overwrite?

No.

We were just discussing the reasons why Python will not and can not have
an assignment operator just a few days ago. Check the archives for more
details.


 class MyInt:
   def __init__(self, val):
   assert(isinstance(val, int))

isinstance() considered harmful:

http://www.canonical.org/~kragen/isinstance/

   self._val = val

Seems kind of pointless. What does MyInt do that ordinary ints don't?
Apart from slow your program down and require extra programming effort.

 a = MyInt(10)
 
 # Here i need to overwrite the assignement operator a = 12

Can't happen. a is just a name, not an object with methods that can be
called. a can be bound to anything, not just MyInt instances. Objects
like MyInt(10) can have no name, one name or many names:

mylist = [0, MyInt(10), 20, 30]  # MyInt instance has no name
x = MyInt(10)  # MyInt instance has one name
x = y = z = MyInt(10)  # MyInt instance has many names



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Jerry


On Oct 17, 8:50 am, Alexander Eisenhuth [EMAIL PROTECTED]
wrote:
 Hello,

 is there a assignement operator, that i can overwrite?

 class MyInt:
 def __init__(self, val):
 assert(isinstance(val, int))
 self._val = val

 a = MyInt(10)

 # Here i need to overwrite the assignement operator
 a = 12

 Thanks
 Alexander

I believe the property function is what you are looking for.  e.g.

class MyClass:
def __init__(self, val):
self.setval(val)

def getval(self):
return self._val

def setval(self, val):
assert(isinstance(val, int))
self._val = val

_val = property(self.getval, self.setval)

--
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Bruno Desthuilliers
Steven D'Aprano wrote:
 On Tue, 17 Oct 2006 15:50:47 +0200, Alexander Eisenhuth wrote:
 
 Hello,

 is there a assignement operator, that i can overwrite?
 
 No.
 
 We were just discussing the reasons why Python will not and can not have
 an assignment operator just a few days ago. Check the archives for more
 details.
 
 
 class MyInt:
  def __init__(self, val):
  assert(isinstance(val, int))
 
 isinstance() considered harmful:

Trying to convert val to an int would probably be better indeed:

class MyInt(object):
  def __init__(self, val):
self.val = int(val)

My 2 cents
-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Bruno Desthuilliers
Jerry wrote:
(snip)
 I believe the property function is what you are looking for. 

It is not.

 e.g.
 
 class MyClass:

Descriptors don't work fine with old-style classes. Should be:

class MyClass(object):

 def __init__(self, val):
 self.setval(val)
 
 def getval(self):
 return self._val
 
 def setval(self, val):
 assert(isinstance(val, int))
 self._val = val
 
 _val = property(self.getval, self.setval)

NameError : self is not defined.
Should be :
_val = property(getval, setval)

but then - since setval() now calls _vals.__set__(), which itself calls
setval(), you have a nice infinite recursion (well, almost infinite -
hopefully, Python takes care of it).

May I kindly suggest that you learn more about properties and test your
code before posting ?-)

Anyway, even with the following correct code, this won't solve the OP's
question:
class MyClass(object):
def __init__(self, val):
self.val = val

def _getval(self):
return self._val

def _setval(self, val):
self._val = int(val)

val = property(_getval, _setval)


m = MyClass(42)
m
= __main__.MyClass object at 0x2ae5eaa00410
m.val
= 42
m = 42
m
= 42
type(m)
= type 'int'


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Alexander Eisenhuth
Wow, thanks a lot for your quick answers.

That assignement is no operator, but a statemant is a pity, but indeed I came 
foward with overwritten methods for numeric types

Regards
Alexander
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Jerry
 class MyClass:Descriptors don't work fine with old-style classes.
Interesting, I have used this construct before in Python 2.4.3 and not
run into the recursion problem you talk about.  Also, it has worked
fine for me.  Perhaps you can post a link to your source so that I
could study it and understand what circumstances my solution works and
what the recommended construct actually is.

 May I kindly suggest that you learn more about properties and test your
 code before posting ?-)
I did test this on Python 2.4.3 in Mac OS X 10.4 and it worked fine.

--
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Bruno Desthuilliers
Jerry wrote:
 class MyClass:Descriptors don't work fine with old-style classes.
 Interesting, I have used this construct before in Python 2.4.3 and not
 run into the recursion problem you talk about.

The recursion problem doesn't occur with you original code (for the good
reason that there's a name error way before). It doesn't even occur when
the cause of the name error is corrected, since the first (explicit)
call to setval() in the __init__ rebind self._val to the value passed -
so the property is in fact *never* used.

 Also, it has worked
 fine for me.  

For a very peculiar definition of works fine !-)

 Perhaps you can post a link to your source

class MyClass:
def __init__(self, val):
self.setval(val)
print in __init__, after setval(): self._val is %s \
   % type(self._val)

def getval(self):
print in getval - you won't see me unless you explicitely call
getval
return self._val

def setval(self, val):
print in setval
self._val = val
print you wont see me no more unless you explicitely call setval

_val = property(getval, setval)


 so that I
 could study it and understand what circumstances my solution works

It doesn't work in any circumstances.

 and
 what the recommended construct actually is.

class MyWorkingClass(object):
def __init__(self, val):
self.val = val

def _setval(self, val):
print _setval to %s % val
self._val = val

def _getval(self):
print _getval
return self._val

val = property(_getval, _setval)


 May I kindly suggest that you learn more about properties and test your
 code before posting ?-)
 I did test this on Python 2.4.3 in Mac OS X 10.4 and it worked fine.

Here's the exact code you posted:

class MyClass:
def __init__(self, val):
self.setval(val)

def getval(self):
return self._val

def setval(self, val):
assert(isinstance(val, int))
self._val = val

_val = property(self.getval, self.setval)

And here's the result:

Traceback (most recent call last):
  File stdin, line 1, in ?
  File /usr/tmp/python-30955cPK.py, line 1, in ?
class MyClass:
  File /usr/tmp/python-30955cPK.py, line 15, in MyClass
_val = property(self.getval, self.setval)
NameError: name 'self' is not defined


HTH
-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Jerry
Okay, very well, then I put a couple of extra 'self' identifiers in
there when I hand-copied the code over.  That would be my mistake for
letting my fingers do the walking and forgetting my brain.  Is there
anything else wrong with my code?

--
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for assignement operator

2006-10-17 Thread Bruno Desthuilliers
Jerry wrote:
 Okay, very well, then I put a couple of extra 'self' identifiers in
 there when I hand-copied the code over. 

You should try copy/paste - it's both safer and less work !-)

 That would be my mistake for
 letting my fingers do the walking and forgetting my brain.  Is there
 anything else wrong with my code?

You mean something I didn't cover in my 2 previous posts ?

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list