Python 3 new features(ibm.com)
ibm.com
Python 3 new features
http://www.ibm.com/developerworks/linux/library/l-python3-1/index.html?ca=dgr-twtrPython3-P1dth-LX&S_TACT=105AGY83&S_CMP=TWDW
11 comments
"True division—for example, 1/2 returns .5."
This wasn't there previously? Ugh.
This wasn't there previously? Ugh.
1/2 used to result in integer division, so it would return 0. In Python 3, integer division is spelled "1//2".
When given two integers pretty much any language will return an integer (so 1/2 = 0).
In Python 2.6, if one of the arguments is a float you'll get a float result, the same is true for Ruby 1.8.
The new behavior is to return a float even if two integers are passed.
In Python 2.6, if one of the arguments is a float you'll get a float result, the same is true for Ruby 1.8.
The new behavior is to return a float even if two integers are passed.
Well, not exactly "pretty much any language": Common Lisp, Scheme, Arc, Clojure, Haskell, Perl, and SQL all return either 1/2 or 0.5. I'm sure there are plenty of others I'm not familiar with that behave the same way.
Good point. I was thinking of the C-based languages, though to be honest I'm not sure all of them behave in the same way. I should have been more careful talking about languages, especially around here...
It would do so if one of the numbers was a float - 1.0/2.0 gave a float answer, 1/2 gave an integer answer.
It was also possible in recent Python versions to switch this behaviour using "from __future__ import division" so they recognised it as a ward, but it's only with the decision to allow Python 3 to make backwards compatibility breaking changes that it could be changed normally.
It was also possible in recent Python versions to switch this behaviour using "from __future__ import division" so they recognised it as a ward, but it's only with the decision to allow Python 3 to make backwards compatibility breaking changes that it could be changed normally.
> It was also possible in recent Python versions
Not that recent, the switch has been there since 2.2 alpha 2, released 8 years ago (August 22, 2001):
Not that recent, the switch has been there since 2.2 alpha 2, released 8 years ago (August 22, 2001):
__all__ = ['all_feature_names', 'nested_scopes', 'generators', 'divisi...
absolute_import = _Feature((2, 5, 0, 'alpha', 1), (2, 7, 0, 'alpha', 0...
all_feature_names = ['nested_scopes', 'generators', 'division', 'absol...
division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192...
generators = _Feature((2, 2, 0, 'alpha', 1), (2, 3, 0, 'final', 0), 0)
nested_scopes = _Feature((2, 1, 0, 'beta', 1), (2, 2, 0, 'alpha', 0), ...
print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...
unicode_literals = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', ...
with_statement = _Feature((2, 5, 0, 'alpha', 1), (2, 6, 0, 'alpha', 0)...Everything's relative. XP was just released in 2001... that was ages ago. Relatively.
see PEP 238, @ http://www.python.org/dev/peps/pep-0238/
print(césar)
Should give arc a run for its money.
Should give arc a run for its money.
Python 3.0 whatsnew http://docs.python.org/3.1/whatsnew/3.0.html
Python 3.1 whatsnew http://docs.python.org/3.1/whatsnew/3.1.html
They're clearer, more readable, more complete and linkable. No reason to use TFA.