[Edu-sig] source code from SA:10648

kirby urner kirby.urner at gmail.com
Fri Jul 16 16:43:12 CEST 2010


On Fri, Jul 16, 2010 at 7:27 AM, Tim Peters <tim.peters at gmail.com> wrote:
> [kirby]
>> ...
>>  Using print as a function is optional in 2.6 without
>> importing anything special.
>
> Nope!  `print` is a statement in all 1.x and 2.x versions of Python.
> When you do, say,
>
>    print(1)
>

Ouch, thank you for clearing up a basic confusion Tim!

I knew this only a month ago when I *did* use

from __future__ import print_function

in another Python 2.6 class, but in the fog of battle
was lapsing into a more punch drunk state.  I could
feel when I was writing that last post that I was
saying something wrong.  But what?

I'm glad to be performing in the role of "dunce clown" here
-- somebody's gotta do it (?), might as well be me.

While I'm being in this mode, here's another basic
question.  Is there a way, after importing from __future__,
to revert to the "old ways" later on in the same script?
Here was my (failed) experiment along those lines:

>>> 1/2
0

>>> from __future__ import division
>>> 1/2
0.5
>>> del division
>>> 1/2
0.5
>>> division

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    division
NameError: name 'division' is not defined
>>> from __future__ import division
>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

I'm not saying this would ever be a good idea, just
wondering about possibilities...

Kirby

> before Python 3.x, you're not calling a print function with an
> argument of 1, you're giving the expression (1) to the print
> statement.  (1) is the same thing as 1, so it just prints "1".
>
> Maybe the difference is clearer here:
>
>    print(1,2,3)
>
> Before 3.x, tjat's the same as
>
>    _x = (1, 2, 3)
>    print _x
>
> That is, it's not passing arguments 1, 2 and 3 to a print function,
> it's giving a single expression - the 3-tuple (1, 2, 3) - to the print
> statement - and is very different from
>
>    print 1, 2, 3
>
> (which gives 3 expressions - not just 1 - to the print statement).
>
> It's really the same as doing, e.g.,
>
>    return(1)
>
> `return` and `print` are statements, and tacking "(1)" on does not
> magically turn either into a function call, it's just adding redundant
> parentheses to the expression "1".
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>


More information about the Edu-sig mailing list