Easy questions from a python beginner

Michael Torrie torriem at gmail.com
Sun Jul 11 14:18:54 EDT 2010


On 07/11/2010 11:48 AM, wheres pythonmonks wrote:
> I'm an old Perl-hacker, and am trying to Dive in Python.  I have some
> easy issues (Python 2.6)
> which probably can be answered in two seconds:
> 
> 1.  Why is it that I cannot use print in booleans??  e.g.:
>>>> True and print "It is true!"

This works in Python 3, or 2.6 and later with the print function:
>>> True and print("It's true!")

That said, this is one particular perl-ism that most python programmers
don't really like.  I think most python programmers prefer:

if True: print "It's true!"

Other perlisms like "something or die" are also to be avoided in python
generally. Just state what you want:  if not something: sys.exit(1)

> I found a nice work-around using eval(compile(.....,"<string>","exec"))...
> Seems ugly to this Perl Programmer -- certainly Python has something better?

Seems ugly even to Python programmers!  And potentially dangerous.  Just
use an if statement.  It's cleaner, more readable, and explicitly
declares what you want.

> 2.  How can I write a function, "def swap(x,y):..." so that "x = 3; y
> = 7; swap(x,y);" given x=7,y=3??
> (I want to use Perl's Ref "\" operator, or C's &).
> (And if I cannot do this [other than creating an Int class], is this
> behavior limited to strings,
>  tuples, and numbers)

I'm not sure you can.  Python "variables" are names that are bound to
objects.  When you pass an object to a function, that object is bound to
the names declared in the function.

Python passes things by object, not by reference.  Names are merely for
convenience when accessing an object.  If you want a function to be able
to modify an object you have to make sure that object is mutable, or if
it's not, pass it in a list (which is mutable).

You probably could do something like this:
(x,y) = (y,x)

Seems pretty clean to me, cleaner than using a function to swap things.

> 3.  Why might one want to store "strings" as "objects" in numpy
> arrays?  (Maybe they wouldn't)?
> 
> 4.  Is there a way for me to make some function-definitions explicitly
> module-local?

The standard way is to declare them with a name beginning with a leading
_ (underscore) to indicate that they are private and should not be
accessed by others.  Someone could access them if they want, though;
python obviously lets programmers shoot themselves in the foot if they want.

> (Actually related to Q3 below: Is there a way to create an anonymous scope?)

Not that I know of.  A nested function usually does enough for me.

> 5. Is there a way for me to introduce a indention-scoped variables in python?
> See for example: http://evanjones.ca/python-pitfall-scope.html

Perhaps nest a function and call it?

> 6.  Is there a Python Checker that enforces Strunk and White and is
> bad English grammar anti-python?  (Only half joking)
> http://www.python.org/dev/peps/pep-0008/



More information about the Python-list mailing list