python / cpython

The Python programming language
https://www.python.org
Other
67.11k stars 31.95k forks source link

reverse method for string objects. #37692

Closed b7ef0382-91c9-45ab-8086-cea1767b3e59 closed 22 years ago

b7ef0382-91c9-45ab-8086-cea1767b3e59 commented 22 years ago
BPO 661281
Nosy @loewis, @rhettinger
Files
  • py23a1-str-reverse.patch: STRING.reverse() implementation.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = created_at = labels = ['interpreter-core'] title = 'reverse method for string objects.' updated_at = user = 'https://bugs.python.org/gminick' ``` bugs.python.org fields: ```python activity = actor = 'gminick' assignee = 'none' closed = True closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'gminick' dependencies = [] files = ['4847'] hgrepos = [] issue_num = 661281 keywords = ['patch'] message_count = 7.0 messages = ['42192', '42193', '42194', '42195', '42196', '42197', '42198'] nosy_count = 3.0 nosy_names = ['loewis', 'rhettinger', 'gminick'] pr_nums = [] priority = 'normal' resolution = 'rejected' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue661281' versions = ['Python 2.3'] ```

    b7ef0382-91c9-45ab-8086-cea1767b3e59 commented 22 years ago
    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 :)
    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 22 years ago

    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.

    rhettinger commented 22 years ago

    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.

    b7ef0382-91c9-45ab-8086-cea1767b3e59 commented 22 years ago

    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 ;)

    rhettinger commented 22 years ago

    Logged In: YES user_id=80475

    For educational purposes, here are a few thoughts.

    Hope this is of some help to you.

    Looking forward to your future contributions.

    61337411-43fc-4a9c-b8d5-4060aede66d0 commented 22 years ago

    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 :-)

    b7ef0382-91c9-45ab-8086-cea1767b3e59 commented 22 years ago

    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 ;)