[Tutor] Better structure?

Alan Gauld alan.gauld at freenet.co.uk
Wed Feb 2 09:52:20 CET 2005


Jacob,

Writing library code is a difficult and unrewarding task
- I've been there so I sympathise, however...

> So, how would one go about this in a non broken code way? Don't they
have
> something like what I'm requesting.

Its not broken, its just different to what you want.
What you want is much simpler than what lstrip does
so you can easily code your requirements.
Try writing the code to do what lstrip actually does
- its much harder. So the library includes the more
difficult function and lets you code the easy ones.

> It seems to me that a few things are flawed in the standard
distribution.

No library is perfect - you should try using C!

But the Python library isually has good reasons for its
apparent limitations.

> Little things like the overlooking of adding a method or function to
decimal
> for returning an instance with x places right of the decimal.

But there is very little need to do that. Usually you only want
to do that in presentation - ie printing. Thus the mechanism
for doing that is in the string formatting code - where it
makes sense. Why would limiting the precision of the internal
number representation be something you'd want to do? Very
occassionally maybe, but in those cases its easy to fake
(and it is a fake because the number is in binary and not
true decimal so its always an approximation - in any language!)

> quantize, but that's junk and not simple.

You mean it does a complex job and not the one youd like it to do? :-)

> Also why shouldn't string methods include stuff like
> lstrip which do precisely what I request?

strings do include lstrip() but as I said they couldn't predict
what you thought it shjould do, so they built it to do what
they thought would be the most generally useful function.
Stripping off a string literal is such a trivial programming
task that you can easily write that yourself. You can even
subclass string and make it a method if you like.

> Maybe because they don't want to bother putting
>
> def mylstrip(string,stripstring):
>     return string[len(stripstring):]
>
> as a method or something?
> Messy, just plain messy.

Such a method would indeed be plain messy.
Do you really want code like this:

>>> s = "Here is a longish string"
>>> s.mylstrip('foo')
>>> print s
e is a longish string
>>> s.mylstrip('supercalifragilisticexpalidocious')
>>> print s

>>>

I suspect your function should probably be:

def mylstrip(str,stripstring):
    if str.startswith(stripstring) and
       len(stripstring) < len(str):
       return str[len(stripstring):]

But thats just my guess at what *you* really want...

And finally remember that Python is built by volunteers mostly.
You should be pleased the *bothered* to write any of it!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


>
> Jacob Schmidt
>
>
> > Jacob S. wrote:
> >
> >> I don't know who's going crazy here... but I checked that
straight from
> >> the python 2.4 interpreter...
> >
> > >>> a = "go on long buddy"
> > >>> a.lstrip('gonl')
> > ' on long buddy'
> > >>> a.lstrip('gonl ')
> > 'buddy'
> > >>>
> >
> > Note that in the second case, I've included a space in the
lstrip()
> > parameter.  lstrip() is essentially saying "remove all leading
characters
> > until you find a character that's not in this sequence" -- 
> > a space counts as a character.  (Took me trying it out before I
saw this,
> > so don't feel bad. ;) )
> >
> > Jeff Shannon
> > Technician/Programmer
> > Credit International
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>
>



More information about the Tutor mailing list