Easy questions from a python beginner
Stephen Hansen
me+list/python at ixokai.io
Sun Jul 11 14:37:45 EDT 2010
On 7/11/10 10: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!"
Because print is a statement. Statements have to start lines. If you
want to do this, use a function-- in Python 2.6 either via "from
__future__ import print_function" or writing your own, even if its just
a very thing wrapper around the print statement.
> 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)
You can't do that*. Its not limited to any certain type of objects. You
can't manipulate calling scopes: if you really want to do that sort of
explicit namespace mangling, use dictionaries (or objects, really) as
the namespace to mangle and pass them around.
> 3. Why might one want to store "strings" as "objects" in numpy
> arrays? (Maybe they wouldn't)?
I don't use numpy. No idea.
> 4. Is there a way for me to make some function-definitions explicitly
> module-local?
In what sense? If you prepend them with an underscore, the function
won't be imported with "from x import *". You can also explicitly
control what is imported in that way with a module-level __all__ attribute.
Now that won't stop someone from doing "import x" and
"x._your_private_function" but Python doesn't believe in enforicng
restrictions.
> (Actually related to Q3 below: Is there a way to create an anonymous scope?)
No. You can create a limited anonymous function with lambda, but note it
takes only an expression-- no statements in it.
> 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. Python only has three scopes historically; local, global, and
builtin. Then post-2.2(ish, I forget) limited nested scoping -- but only
with nested functions, and you can't (until Python 3) re-bind variables
in outer scopes (though you can modify them if they are mutable objects).
Python's scoping is very basic (we generally think this is a good thing;
others are never happy with it) and is not fully lexical scoped.
> 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/
Check out pylint and/or pychecker, which do various style-based
checking. If you're asking for something else, I can't pierce your
sarcasm to figure out what.
--
Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/
* Yes, I know its actually possible, to manipulate outer/calling scopes
with frame hacking. This is dangerous / bad / an implementation detail
that one should not rely on or use, generally speaking. If you need to
do this you're writing Java or Perl or C in Python, instead of writing
Python in Python, so are probably doing all kinds of things that are
slow / bad / dangerous / just not taking advantage of Python's strengths.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100711/a1aef088/attachment-0001.sig>
More information about the Python-list
mailing list