Few things

Josiah Carlson jcarlson at uci.edu
Fri Nov 26 06:41:10 CET 2004


bearophileHUGS at lycos.com (bearophile) wrote:
> 
> Hello,
> here are a four more questions (or suggestions) for the language
> (probably people have already discussed some of/all such things:
> 
> I've seen the contracts for Python:
> http://www.wayforward.net/pycontract/
> http://www.python.org/peps/pep-0316.html
> They look interesting and nice, how Python developers feel about
> accepting something like this in the standard language? (Maybe they
> are a bit complex).

Decorators can do this without additional syntax.  Think @accepts and
@returns.


> I think it can be useful a little stat standard module that computes
> permutations, combinations, median (quickselect), etc. There is even a
> C implementation (of most of them):
> http://probstat.sourceforge.net/
> Probably some Win users can appreciate to have this already compiled
> (and built in).

Having a 'faq' for permutation and combination generation would be 99%
of the way there.  Quickselect, really, doesn't gain you a whole lot. 
Sure, it's a log factor faster to select a median, but many algorithms
involving selecting medians (at least the ones that I run into in CS
theory) end up repeatedly (logn) time selecting the 'kth' smallest
element (varying k's), where sorting would actually run slightly faster.

As for the rest of it, be specific with what you would want to be in
this mythical 'statistics' module ('stat' is already used for the
filesystem stat module).  A single-pass average/standard deviation has
already been discussed for such a module, as well as give me all the
k-smallest items of this sequence, etc., but was tossed by Raymond
Hettinger due to the limited demand for such a module.


> A command like this:
> print 0x9f, 054135
> This prints an hex and octal. I think the syntax for the hex is a bit
> ugly; and the syntax for the octal looks just dangerous (and wrong) to
> me.

Internally those values are Python integers, there would need to be a
special way to tag integers as being originally hex or octal.  Or the
pyc would need to store the fact that it was originally one of those
other methods specifically for the print statement.

The preferred way for doing such things (printing some internal type via
some special method) is via string interpolation:
print "0x%x 0%o"%(0x9f, 054135)

Ugly or not, "Special cases aren't special enough to break the rules." 
Don't hold your breath for print doing anything special with integers.


> In some python source codes that I'm finding around, I find things
> like:
> def foo():
>     '''This is just a
> silly text'''
> ...
> 
> Because:
> def foo():
>     '''This is just a
>        silly text'''
> print foo.__doc__
> 
> Outputs:
> This is just a
>        silly text
> 
> I think a better syntax for such multiline strings can be something
> like: remove from all the beginnings of the lines successive to the
> first one a number of spaces equal to the position of ''' in the
> soucecode.
> With this sintax such print outputs:
> This is just a
> silly text
> 
> Note: even less indentation of the lines successive the first one can
> be simply ignored:
> def foo2():
>     '''This is just a
>      silly text'''
> print foo.__doc__
> 
> Outputs:
> This is just a
> silly text

It is a wart.  An option is to use:
def foo():
    '''\
This is just a
silly text'''

Me, I just don't use docstrings. I put everything in comments indented
with the code.  I have contemplated writing an import hook to do
pre-processing of modules to convert such comments to docstrings, but I
never actually use docstrings, so have never written the hook.


 - Josiah




More information about the Python-list mailing list