3 ms·
>For example: you can't assign the result of an "if" statement. Why? Because it's a SyntaxError, no real reason. There is even an "ifExp" in the language gramma
by throwaway_374 10y ago
>For example: you can't assign the result of an "if" statement. Why? Because it's a SyntaxError, no real reason. There is even an "ifExp" in the language grammar, but that's only for inline ifs.
Do you mean something like a ternary operator? You can certainly do:
x='foo' if condition else 'bar'.
In fact you can even do multiple assigments:
(x,y) = ('foo1','foo2') if condition else ('foo3', 'foo4')
where 'Foo' may also be a function call.
>I think people feel like it's unpythonic and instead pollute the namespace with singleton classes that only have one method.
There's a few pycon talks on "a single method class should not be a class" but I'd say that's the single responsibility principle at its purest along with the community needing to move to more lightweight structures like namedtuples.