[Patches] [ python-Patches-661281 ] reverse method for string objects.

noreply@sourceforge.net noreply@sourceforge.net
Fri, 03 Jan 2003 01:24:25 -0800


Patches item #661281, was opened at 2003-01-02 21:06
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=661281&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Closed
Resolution: Rejected
Priority: 5
Submitted By: Wojtek Walczak (gminick)
Assigned to: Nobody/Anonymous (nobody)
Summary: reverse method for string objects.

Initial Comment:
Well, in python 2.3a1 you can reverse a string in dozens of ways, for example:
>>> a = 'string'
>>> a[::-1]
'gnirts'
>>> reduce(lambda x,y: y+x, a)
'gnirts'
>>> b = list(a)
>>> b.reverse()
>>> "".join(b)
'gnirts'
but there's no simple method as function reverse() in plenty of languages. That patch changes that situation by adding reverse method to Objects/stringobject.c
Tested on Linux 2.4.5 with Python2.3alpha1 with i586 platform.
Feel free to improve that or throw it away :)

----------------------------------------------------------------------

>Comment By: Wojtek Walczak (gminick)
Date: 2003-01-03 10:24

Message:
Logged In: YES 
user_id=679226

Big thanks Raymond, your help is really appreciated.
See you in the next patch discussion, but next time I'll do my homework ;)

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-01-03 10:01

Message:
Logged In: YES 
user_id=21627

I think the patch got reopened as Raymond's and Woijtek's
edits have crossed; last writer wins on SF. So closing it again.

Wojtek, there are really many ways to contribute. I see
three (not necessarily identical) sources of ideas:

1. Do what Python maintainers would appreciate.
    Look at the existing feature requests, and try to
provide those 
    features. Look at the existing bug reports, and try to
fix those 
    bugs.

2. Do what users would want to see.
    For that, you probably have to watch comp.lang.python; if 
    somebody has some idea which isn't outright rejected by
    others, see whether you can work with the poster to provide 
    that feature.

3. Implement what you would use yourself
   I'm not sure whether the string.reverse would fall into that
   category; I'm rather thinking that if you find annoyances
when
   writing Python applications, you could consider contributing.
   In some cases, it might still be better to solve the problem 
   outside Python; it takes some experience to learn when
   modifying Python is appropriate (and opinions vary. Hi 
   Rayomond :-)

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-01-03 09:51

Message:
Logged In: YES 
user_id=80475

For educational purposes, here are a few thoughts.

* the malloc for "old" is not used and the memory for it is 
permanently lost when overwritten by 
PyString_AS_STRING.

* PyString_AS_STRING returns a pointer to an existing 
buffer which should not be altered, reference counted, or 
freed (per 7.3.1 of the C API manual).

* In general, malloc() should not be used, PyMem_Alloc 
and Free are their Python managed counterparts.

* Use SF's search box (under feature requests) to find 
previous discussions and reviews.  This will make sure 
that your not proposing an idea that has already been 
discussed and rejected.  In this case, see feature request 
494240.

* A good way to get started making contributions is to 
study and make review comments on other people's 
patches or to propose bug fixes.

* FYI, the discussions can continue even after a patch is 
closed (it doesn't need to be re-opened for that).

Hope this is of some help to you.

Looking forward to your future contributions.





----------------------------------------------------------------------

Comment By: Wojtek Walczak (gminick)
Date: 2003-01-03 09:29

Message:
Logged In: YES 
user_id=679226

loewis wrote:
> What other languages have builtin reversal of strings?
For example ruby ;] (...but, well, looks like it really 
isn't too important method, i'll try to think about something
more useful). 

> In addition, your patch is wrong: it leaks len bytes per 
> call.
OK, can you tell me how to change that?

ps. in the world of python developers I'm new, so hi everyone  ;)

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-01-03 09:18

Message:
Logged In: YES 
user_id=80475

This has been proposed and rejected before.  The reasons 
were lack of non-toy use cases (though somebody actually 
found one or two real world examples), its value as 
classroom exercise, and avoiding interface bloat.

Now, with the "string"[::-1], there is even less of a case for 
reverse (TOOWTDI).

With apologies, marking as rejected and closing the 
patch.  Future patches and bug fixes are always 
welcomem as are any efforts to contribute.  Even if a 
patch doesn't get accepted, it is appreciated.


----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-01-03 00:25

Message:
Logged In: YES 
user_id=21627

-1. How often do you need to reverse a string, outside
assignments for programming course? For the latter purpose,
it would actually hurt if Python provided a method for that.

What other languages have builtin reversal of strings?

In addition, your patch is wrong: it leaks len bytes per call.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=661281&group_id=5470