Syntax across languages
Fredrik Lundh
fredrik at pythonware.com
Sun Oct 23 03:52:34 EDT 2005
bearophileHUGS at lycos.com wrote:
> - Information about the current line and file as Ruby:
> __LINE__ __FILE__
> Instead of the python version:
> inspect.stack()[0][2] inspect.stack()[0][1]
(that's (mostly) CPython-dependent, and should be avoided)
> - ~== for approximate FP equality
str(a) == str(b)
> - comparison returns 4 values (i.e. inferior, equal, superior or not
> comparable), as in Pliant: "compare"
>>> cmp("a", "b")
-1
>>> cmp("a", "a")
0
>>> cmp("b", "a")
1
>>> cmp("ä", u"ä")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte /.../
sure looks like four possible outcomes.
> - identity function: "identity" as in Common Lisp (probably of little
> use in Python).
id(a)
> - Exception retrying: after catching an exception, tell the snippet to
> be re-run "retry" as in Ruby
>>> x = 0
>>> while 1:
... try:
... x += 1
... if x <= 5:
... raise ValueError
... except ValueError:
... print "retry"
... continue
... else:
... break
...
retry
retry
retry
retry
retry
>>>
> - object cloning: obj.copy() obj.deepcopy()
import copy
(cloning is usually a sign of a design problem in python. if you think
you need it, you probably don't. if you really think you need it, import
copy.)
> - recursive "flatten" as in Ruby (useful)
if you can define the semantics, it's a few lines of code. if you're not
sure about the semantics, a built-in won't help you...
etc.
</F>
More information about the Python-list
mailing list