Easy questions from a python beginner

Thomas Jollans thomas at jollans.com
Sun Jul 11 15:09:57 EDT 2010


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



More information about the Python-list mailing list