Easy questions from a python beginner

Stephen Hansen me+list/python at ixokai.io
Sun Jul 11 15:00:52 EDT 2010


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/

-------------- 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/1758f92f/attachment-0001.sig>


More information about the Python-list mailing list