New to Python: Features

Alex Martelli aleaxit at yahoo.com
Tue Oct 5 03:34:32 EDT 2004


Andrew Dalke <adalke at mindspring.com> wrote:
   ...
> Like I said with eval, DO NOT USE THIS!  It almost certainly
> means you're doing the wrong thing.  It's full of security
> holes and there are nearly always better ways to solve
> the underlying problem.

Applause and full agreement.

> > I know but that's a real bummer.  No way around that?
> 
> Python is not a functional programming language.
> 
> Do you want it because it's cool or because it's useful?

I'd bet a decorator to munge a function or method (by bytecode hacking)
into lean tail-call won't be long emerging.  My use case for this was
the nightmare of 'recursion limits exceeded' for a nevow page with a few
hundred deferreds -- they got (implicitly, by the framework) chained to
each other by tail call (for termination purposes), and besides the
orrid waste of time in building all those frames for nothing, the
exception-within-an-exception was hell to debug.  There are workarounds,
of course, but being able to code the obvious solution directly would be
useful.

> Python doesn't support Smalltalk's system image.  That's
> probably a good thing.

I was very used to workspaces back in the days of APL and I still miss
them.  Why is it good not to have them?


> > I'd like to be able to use a string as an integer without an explicit
> > coerion on my part for example.
> 
> NO!  Don't do that.  Bad code.  Subtle errors.
> 
> Try Perl or Ruby instead.

Not Ruby!  That's a _good_ language:

kallisti:~/cb/cha/tex/cba alex$ ruby         
2+"23"
-:1:in `+': String can't be coerced into Fixnum (TypeError)
        from -:1

to perpetrate such horrors, he definitely wants Perl.


> > That is quite a shame, no modules for this either huh?
> 
> It quite depends on your expectations of what design by
> contract, protocols, etc. means.  Do some research.  Hint:
> Google for 'python "design by contract"' and related topics.

'python protocols' and/or adaptation is quite a different (but related)
tack also worth googling for.

> >>> 43. Embedding variables in strings like: print "Hello, World. Time:
> >>> #{Time.now}"
> >>
> >> t = time.asctime()
> >> print "Hello World %(t)s"%{'t':t}
> >> print "Hello World %(t)s"%locals()
> > 
> > That is messy, exactly why I wanted the kind of clear embedding I gave
> > an example for.  Oh well.
> 
> You asked for "embedding variables" not embedding expressions.
> You got what you asked for, not what you wanted.  That's part
> of being able to ask the right question.  Remember that essay
> people suggested you read?

Well, in 2.4 w/string.Template he does get variables specifically (with
nicer syntax), but if the '%(...)s' syntax ain't too messy for him
interpolating expressions is still possible (maybe not _wise_...):

>>> class map:
...   def __getitem__(self,name): return eval(name)       
... 
>>> print 'hello world at %(time.asctime())s' % map()
hello world at Tue Oct  5 09:22:52 2004

Not sure what docs (besides Python Cookbook and Python in a Nutshell)
one would have to read to find this answer -- differently from most
other questions he's asking, this one's hardly obvious from docs...

 
> > Really?  Hmmmm, does anyone know of a bridge between Python and Ruby?
> 
> Why not just use Ruby?  It seems to fit your search criteria
> much better.

I agree with you that it does, though not as well as you appear to
believe (e.g. no `` 2 + "23" '' abominations in Ruby, either).  Perl
might be better still for what he's asking -- said abomination does
work, he gets to mess with syntax (google for lingua latina perligata),
and so forth.


Alex



More information about the Python-list mailing list