This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paalped
Recipients eitan.adler, paalped, r.david.murray, rhettinger, serhiy.storchaka, terry.reedy
Date 2018-05-26.11:01:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <CAOp859dpBqkK_2M4xO6NTEeqr9H8wCov4GhQL3SpwZ7gtnQknQ@mail.gmail.com>
In-reply-to <1527294078.4.0.682650639539.issue33647@psf.upfronthosting.co.za>
Content
I forgot to put the ** in the input.

This is to further explain what I had in mind:

"""The meaning of this file is to show how the replace function can be
enhanced"""

# Today the replace api takes at most 3 arguments, old_string,
new_string, and optional numbers_of_replacements
# My wish is to update the replace to support a fourth optional
**kwargs argument

class Str(object):
    def __init__(self, s=''):
        self.original_str = s
        self.new_str = s
    # name replace2 is to not cause recursion of my function
    def replace2(self, *args, **kwargs):
        # Old api lives here ...
        if len(args) == 2:
            old_str, new_str = args
            # do the old stuff
        elif len(args) == 3:
            old_str, new_str, numbers_of_replacements = args
            #do the old stuff
        # new api begins
        if kwargs:
            for k, v in kwargs.items():
                # this one relies on the old api from string object
                # str original function must differ
                self.new_str = self.new_str.replace(k, v)
            return self.new_str

    def __str__(self):
        return self.original_str

s = Str('my string is not cool')

replacement_dict = {'is': 'could',
            'my': 'your',
            'not': 'be',
            'cool': 'awsome'}

print(s)
s2 = s.replace2(**replacement_dict)
print(s, s2, sep='\n')

If this is a really crappy though, I would like not to be made a fool out
of. I only want to improve, and I'm not crtizing the old api. I just want a
simplier life for everyone :)

Paal

lør. 26. mai 2018 kl. 02:21 skrev Raymond Hettinger <report@bugs.python.org
>:

>
> Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
>
> > I would like an example added to the sub(pattern, function,
> > string) examples.
>
> That seems like the most reasonable way to go.
>
> ----------
> nosy: +rhettinger
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue33647>
> _______________________________________
>
History
Date User Action Args
2018-05-26 11:01:36paalpedsetrecipients: + paalped, rhettinger, terry.reedy, r.david.murray, serhiy.storchaka, eitan.adler
2018-05-26 11:01:36paalpedlinkissue33647 messages
2018-05-26 11:01:36paalpedcreate