From: wheres pythonmonks on
Thanks for your answers -- it is much appreciated.

On #1: I had very often used chained logic with both logging and
functional purposes in Perl, and wanted to duplicate this in Python.
"It reads like english" Using the print_ print wrapper works for me.

Follow-up:
Is there a way to define compile-time constants in python and have the
bytecode compiler optimize away expressions like:

if is_my_extra_debugging_on: print ...

when "is_my_extra_debugging" is set to false? I'd like to pay no
run-time penalty for such code when extra_debugging is disabled.

On #2: My point regarding the impossibility of writing the swap
function for ints is to explicitly understand that this isn't
possible, so as not to look for solutions along those lines when
trying to write python code.

On #3: Sorry this is confusing, but I was browsing some struct array
code from numpy, in which one of the columns contained strings, but
the type information, supplied in numpy.array's dtype argument,
specified the type as a an "object" not a string. Just wondering why
one would do that.

On #4: So there are some hacks, but not something as easy as "import
unimportable" or an @noexport decorator. The underscore works, so
does "del".

On #5: Nesting the function was actually what I was thinking of doing,
but alas, I cannot modify outer-scope variables within a function, and
of course, I don't want to use globals.

On #6: Always trying to improve my writing -- and I thought it was
cute that Guido tries to encourage this as well.


I am programmer who likes to scope off variables as much as possible
(I believe in minimal state).

The following is an example of what I am trying to protect against:
http://stackoverflow.com/questions/938429/scope-of-python-lambda-functions-and-their-parameters

Will try to avoid namespace mangling until next week.

Thanks again,

W



On Sun, Jul 11, 2010 at 2:17 PM, Duncan Booth
<duncan.booth(a)invalid.invalid> wrote:
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: Stephen Hansen on
On 7/11/10 11:45 AM, wheres pythonmonks wrote:
> Follow-up:
> Is there a way to define compile-time constants in python and have the
> bytecode compiler optimize away expressions like:
>
> if is_my_extra_debugging_on: print ...
>
> when "is_my_extra_debugging" is set to false? I'd like to pay no
> run-time penalty for such code when extra_debugging is disabled.

Any code wrapped in a __debug__ guard is utterly ommitted if you run
Python with the -O option. That, and asserts go away.

> On #2: My point regarding the impossibility of writing the swap
> function for ints is to explicitly understand that this isn't
> possible, so as not to look for solutions along those lines when
> trying to write python code.

Its impossible because Python's calling and namespace semantics simply
don't work like that. There's no references in the traditional sense,
because there's no variables-- boxes that you put values in. There's
just concrete objects. Objects are passed into the function and given
new names; that those objects have names in the enclosing scope is
something you don't know, can't access, and can't manipulate.. even the
objects don't know what names they happen to be called.

Check out http://effbot.org/zone/call-by-object.htm

> On #3: Sorry this is confusing, but I was browsing some struct array
> code from numpy, in which one of the columns contained strings, but
> the type information, supplied in numpy.array's dtype argument,
> specified the type as a an "object" not a string. Just wondering why
> one would do that.

Strings are objects.

I don't use numpy, btu I'd assume "object" would basically mean,
"anything can go here", as everything is an object.

> On #5: Nesting the function was actually what I was thinking of doing,
> but alas, I cannot modify outer-scope variables within a function, and
> of course, I don't want to use globals.

You can modify outer-scope objects: if they are mutable. I.e., a
dictionary. What you can't do is modify outer *scopes*, the namespaces.
You can't re-bind a new/different object to a certain name in an outer
scope.

> I am programmer who likes to scope off variables as much as possible
> (I believe in minimal state).

That's a fine and nice goal. In Python your only real tool for this is
to break things up into more logical functions.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/

From: Thomas Jollans on
On 07/11/2010 08:45 PM, wheres pythonmonks wrote:
> Thanks for your answers -- it is much appreciated.
>
> On #1: I had very often used chained logic with both logging and
> functional purposes in Perl, and wanted to duplicate this in Python.
> "It reads like english" Using the print_ print wrapper works for me.
>
> Follow-up:
> Is there a way to define compile-time constants in python and have the
> bytecode compiler optimize away expressions like:
>
> if is_my_extra_debugging_on: print ...
>
> when "is_my_extra_debugging" is set to false? I'd like to pay no
> run-time penalty for such code when extra_debugging is disabled.

no.

> On #3: Sorry this is confusing, but I was browsing some struct array
> code from numpy, in which one of the columns contained strings, but
> the type information, supplied in numpy.array's dtype argument,
> specified the type as a an "object" not a string. Just wondering why
> one would do that.

No expert on numpy, but maybe storing object references is cheaper than
storing strings here ?

>
> On #5: Nesting the function was actually what I was thinking of doing,
> but alas, I cannot modify outer-scope variables within a function, and
> of course, I don't want to use globals.

yes you can. Well, at least since whenever the "nonlocal" keyword was
introduced (recent, might be 3.x only)

Or you can wrap what you want to change in a dict or list.

> I am programmer who likes to scope off variables as much as possible
> (I believe in minimal state).
>
> The following is an example of what I am trying to protect against:
> http://stackoverflow.com/questions/938429/scope-of-python-lambda-functions-and-their-parameters

On the other hand, python scoping and namespace rules, while they may be
different to those in other languages, are nice and simple.

> Will try to avoid namespace mangling until next week.


Cheers

- Thomas
From: Alf P. Steinbach /Usenet on
* Stephen Hansen, on 11.07.2010 21:00:
> On 7/11/10 11:45 AM, wheres pythonmonks wrote:
>> Follow-up:
>> Is there a way to define compile-time constants in python and have the
>> bytecode compiler optimize away expressions like:
>>
>> if is_my_extra_debugging_on: print ...
>>
>> when "is_my_extra_debugging" is set to false? I'd like to pay no
>> run-time penalty for such code when extra_debugging is disabled.
>
> Any code wrapped in a __debug__ guard is utterly ommitted if you run
> Python with the -O option. That, and asserts go away.
>
>> On #2: My point regarding the impossibility of writing the swap
>> function for ints is to explicitly understand that this isn't
>> possible, so as not to look for solutions along those lines when
>> trying to write python code.
>
> Its impossible because Python's calling and namespace semantics simply
> don't work like that. There's no references in the traditional sense,
> because there's no variables-- boxes that you put values in. There's
> just concrete objects. Objects are passed into the function and given
> new names; that those objects have names in the enclosing scope is
> something you don't know, can't access, and can't manipulate.. even the
> objects don't know what names they happen to be called.
>
> Check out http://effbot.org/zone/call-by-object.htm

Oh, I wouldn't give that advice. It's meaningless mumbo-jumbo. Python works like
Java in this respect, that's all; neither Java nor Python support 'swap'.

Of course there are variables, that's why the docs call them variables.

We've had this discussion before and I know from that that it is a religious
issue with a small subset of the Python community, where reason, facts, logic
does not apply and is not even recognized as such. So be it. So I'm not out to
convince you or other of that sub-community, or trying to reason with you folks
on this issue (futile, and generates flames pretty fast), but I do not want
newbies brainwashed into that non-reasoning nonsense pure faith religion.

For what it's worth, I'm sure that the effbot.org author, whose pages are
otherwise quite technically meaningful & useful, in this case, after the flame
war with some Java folks, decided that technical accuracy just wasn't worth it.

So, I believe, he punted, which is an eminently rational choice when one's goals
require acceptance in a society dominated by a religious clique. And just as I'm
not out to engage you in any debate on this issue (futile), neither am I calling
you irrational. Perhaps your choice is the same as that author's.


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
From: Carl Banks on
On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo...(a)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