S
SpreadTooThin
Can these operators be overloaded?
If so. How?
If so. How?
Can these operators be overloaded?
Yes.
If so. How?
If so. How?
Yes.
Tim said:With the caveat of the "=" mentioned in the subject-line (being
different from "==")...I haven't found any way to override
assignment in the general case. There might be some oddball way
to do it via property() but AFAIK, this only applies to
properties of objects, not top-level names/variables. I'd love
to know if there's some workaround for this though...
With the caveat of the "=" mentioned in the subject-line (being
different from "==")...I haven't found any way to override
assignment in the general case.
Why would you want to do that?
For the same reason one would use property() to create
getter/setter functions for a particular variable--to intercept
attempts to set a variable.
I'm not sure there's an elegant way
to do it other than creating a custom container object with a
getter/setter using property().
That's because assignment isn't an operator - that's why (for example)Tim said:With the caveat of the "=" mentioned in the subject-line (being
different from "==")...I haven't found any way to override
assignment in the general case. There might be some oddball way
to do it via property() but AFAIK, this only applies to
properties of objects, not top-level names/variables. I'd love
to know if there's some workaround for this though...
Daniel said:
Despite sloppy talk to the contrary (which I think most of us do from time
to time), Python doesn't have variables. It has names and objects. Names
are just labels -- there is no difference in behavior between the *names*
this_is_an_integer and this_is_a_string. (The *objects* they point to are
a different story, naturally.)
Steven said:(snip)For the same reason one would use property() to create
getter/setter functions for a particular variable--to intercept
attempts to set a variable.
Suppose we bind the name "x" to the object 1, and then rebind the name "x"
to the object []. Which object's hypothetical __assign__ method should get
called?
Bruno said:Steven said:(snip)With the caveat of the "=" mentioned in the subject-line (being
different from "==")...I haven't found any way to override
assignment in the general case.
Why would you want to do that?
For the same reason one would use property() to create
getter/setter functions for a particular variable--to intercept
attempts to set a variable.
Suppose we bind the name "x" to the object 1, and then rebind the name "x"
to the object []. Which object's hypothetical __assign__ method should get
called?
The current namespace object, of course.
Bruno Desthuilliers said:The current namespace object, of course.
I honestly don't see why "variable" would be an inappropiate word to use.
AFAIU, python assignment seems to behave much like lisp and smalltalk
and I never heard that those communities found the word "variable"
inappropiate to use. And since the word variable originally comes
from mathematics and IMHO the mathematical semantics are closer
to the lisp/smalltalk/python semantics than the C/algol/pascal/ada
semantics I don't see why "variable" is seen as "sloppy talk"
Roman said:People who complain often fail to see how
x = foo()
while x:
process(x)
x = foo()
is safer than
while x = foo():
process(x)
Fredrik Lundh said:or for the perhaps-overly-clever hackers,
for x in iter(lambda: foo() or None, None):
process(x)
Fredrik Lundh said:or for the perhaps-overly-clever hackers,
for x in iter(lambda: foo() or None, None):
process(x)
.... print xfor x in takewhile(foo() for _ in repeat(None)):
process (x)
for x in takewhile(bool, (foo() for _ in repeat(None))):
process(x)
Meh, both are ugly.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.