Easy questions from a python beginner

Carl Banks pavlovevidence at gmail.com
Sun Jul 11 16:52:34 EDT 2010


On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo... at gmail.com>
wrote:
> I'm an old Perl-hacker, and am trying to Dive in Python.

Welcome to the light.


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

I'll repeat other people's sentiments: if you drop nothing else from
your perl habits, drop this one.


> 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)

Can't do it, but you can get reference-like behavior if you don't mind
a level of indirection.  For example:

def swap(x,y):
    t = y[0]
    y[0] = x[0]
    x[0] = t

a = [1]
b = [2]
swap(a,b)

There's no reason to do this for a swap() function, as you've already
seen.  But it is sometimes handy for other things. (This includes
certain idioms involving regualar expression that are common in Perl.
Perl uses some special semantics when performing regexp searches that
allow automatic binding of match results.  Python doesn't, so in some
cases it's helpful to define an object you can mutate to store that
information.)

In the more general sense, Python functions and methods that mutate
the object they operate on are pretty common.


> 3.  Why might one want to store "strings" as "objects" in numpy
> arrays?  (Maybe they wouldn't)?

numpy is a library designed mainly to store numerical data, with some
advanced slicing operations and other cool stuff like
multdimensionality.

Some people, however, want to use the cool slicing operations on
arrays of Python objects, so numpy has a storage mode for arbitrary
Python objects.

(If you're wondering why they use object instead of a character type,
it's because rows of such an array are fixed-length strings.  Main
reason to use those is to interface with Fortran string arrays, or
maybe to simulate crossword puzzles or something.)


> 4.  Is there a way for me to make some function-definitions explicitly
> module-local?
> (Actually related to Q3 below: Is there a way to create an anonymous scope?)

No.  The (loose) convention is to define local or private functions
with a leading underscore to label them as "intended for internal use
only".


> 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

Yes, define a nested function.  In Python 2 it's limited since you
cannot rebind variables in the surrounding scope; that's possible in
Python 3, though.


> 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/

There's a few, PyChecker and PyLint get mentioned a lot.  I used to
use them until I noticed that it never actually caught anything
(except stuff I didn't care about like using "id" as a variable name).


Carl Banks



More information about the Python-list mailing list