Prev: Why doesn't python's list append() method return the list itself?
Next: Getting started with python on macintosh snow leopard with mysql - need help
From: wheres pythonmonks on 11 Jul 2010 13:48 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? 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) 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? (Actually related to Q3 below: Is there a way to create an anonymous scope?) 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 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/ Thanks, W
From: Thomas Jollans on 11 Jul 2010 14:08 On 07/11/2010 07:48 PM, 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!" prior to Python 3.0, print is a statement, not an expression, which means that it's rather unflexible. In Python 3.x, print is a function, and you can use it as one: Python 3.1.2 (release31-maint, Jul 8 2010, 09:18:08) [GCC 4.4.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True and print('It is true!') 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? Either use good old if: if True: print 'It is true!' or use a function instead of the print statement: def print_(s): print s True and print_("It is true!") > > 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. A function cannot modify the caller's namespace. "x" and "y" are just names, not pointers you can edit directly. But look at this: >>> x = 1 >>> y = 2 >>> x,y = y,x >>> x 2 >>> y 1 >>> > 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?) Do you mean have the function visible inside the module, but not to the importer? Well, you could "del thefunc" at the end of the module, but then you won't be able to access it from within other functions. You could in principle create a function, pass it as a default argument to every function that uses it, and then delete it. But that's just silly. > > 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. exception: nested functions. if you really want, you can always del variables after use. > > 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/ Huh? - Thomas
From: Duncan Booth on 11 Jul 2010 14:17 wheres pythonmonks <wherespythonmonks(a)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.
From: Michael Torrie on 11 Jul 2010 14:18 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/
From: Stephen Hansen on 11 Jul 2010 14:37
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. |