Easy questions from a python beginner
Duncan Booth
duncan.booth at invalid.invalid
Sun Jul 11 14:17:49 EDT 2010
wheres pythonmonks <wherespythonmonks at gmail.com> 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!"
>
> I found a nice work-around using
> eval(compile(.....,"<string>","exec"))... Seems ugly to this Perl
> Programmer -- certainly Python has something better?
In Python 2.x print is a statement. If you really wanted you could do:
True and sys.write("It is true!\n")
In Python 3 you can do this:
True and print("It is true!")
though I can't think of any situations where this would be better that just
writing:
if somecondition: print "whatever"
>
> 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??
Why use a function?
x, y = y, x
> (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)
If you want to use perl's operators I suggest you use perl.
>
> 3. Why might one want to store "strings" as "objects" in numpy
> arrays? (Maybe they wouldn't)?
Why would one want to write incomprehensible questions?
>
> 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?)
Not really.
>
> 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
No. The page you reference effectively says 'my brain is used to the way
Java works'. *My* brain is used to the way Python works. Who is to say
which is better?
>
> 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/
>
pylint will do quite a good job of picking over your code. Most people
don't bother.
More information about the Python-list
mailing list