Tobias Weber wrote:
Hi,
being new to Python I find remarkable that I don't see any side effects.
That's especially true for binding. First, it is a statement, so this
won't work:
if x = q.pop():
print x # output only true values
Second, methods in the standard library either return a value OR modify
the reciever, so even if assignment was an expression the above wouldn't
work.
Only it still wouldn't, because IF is a statement as well. So no ternary:
x = if True: 5 else: 7;
However there is one bit of magic, functions implicitly return None. So
while the following will both run without error, only one actually works:
x = 'foo'.upper()
y = ['f', 'b'].reverse()
Now I mentioned that the mutable types don't have functions that mutate
and return something, so I only have to remember that...
But I'm used to exploiting side effect, and sometimes forget this rule
in my own classes. IS THERE A WAY to have the following produce a
runtime error?
def f():
x = 5
# no return
y = f()
Maybe use strict ;)
There's a ternary operator. Check out the following sequence:
a = 42
b = 12 if a == 42 else 9
print a, b
Then try it again with a different value of a.
As for using = in an ordinary expression, I don't really miss it.
Python allows multiple assignments in the same statement, but they're
all to the same object. For the kinds of things where I would have done
an assignment inside an if or while (in C++), I usually can use a list
comprehension or somesuch.
--
http://mail.python.org/mailman/listinfo/python-list