Easy questions from a python beginner

wheres pythonmonks wherespythonmonks at gmail.com
Thu Jul 22 18:34:11 EDT 2010


Okay -- so I promised that I would try the namespace mangling
approach, and here's what I have come up with:

Approach #1:  Pass in the variables to be swapped as strings.  (boring)

>>> import sys
>>> def swap(n1,n2):
...  try:
...   raise RuntimeException()
...  except:
...   e,b,t = sys.exc_info()
...  ldict = t.tb_frame.f_back.f_locals
...  t = ldict[n1];
...  ldict[n1] = ldict[n2]
...  ldict[n2] = t
...
>>> x = 'A'
>>> y = 'B'
>>> id(x)
47946650437696
>>> id(y)
47946649710192
>>> swap('x','y')
>>> print id(x)
47946649710192
>>> print id(y)
47946650437696
>>> print x,y
B A

Approach #2:  Allow the user to pass in arbitrary objects, takes the
id, infer what the variable in by hashing all possible objects, and
then apply the swap operation above.

>>> def swap2(o1,o2):
...  try:
...   raise RuntimeException()
...  except:
...   e,b,t = sys.exc_info()
...  ldict = t.tb_frame.f_back.f_locals
...  iddict = dict( (id(v), k ) for k,v in ldict.items() )
...  # print id(o1), id(o2)
...  n1 = iddict[id(o1)]
...  n2 = iddict[id(o2)]
...  t = ldict[n1];
...  ldict[n1] = ldict[n2]
...  ldict[n2] = t
...
>>> print x,y
B A
>>> swap2(x,y)
>>> print x,y
A B
>>>

Now, I want to make the above codes more "Pythonic" -- is there a way to:

1.  Get the function's arguments from the perspective of the caller?

def f(x):
  print "caller's view of x = %s" % callersview(x)

Then, f(1+2+3) would yield:
caller's view of x = 1 + 2 + 3

2.  Is there a better way to loopup by id?  I'm not very familiar with
sys.exc_info, but creating the id->name hash each time seems like
overkill.

3.  Is there a reference on all the special variables, like __foo__?

4.  Is there any work on deparsing (like Perl's deparse) lambda
functions to inline algebra and get a performance gain?

Thanks again for your input,

W

( from Perl-hacker to Python Programmer )

On Sun, Jul 11, 2010 at 2:37 PM, Stephen Hansen
<me+list/python at ixokai.io> wrote:
> 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.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



More information about the Python-list mailing list