Message317736
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>
> _______________________________________
> |
|
Date |
User |
Action |
Args |
2018-05-26 11:01:36 | paalped | set | recipients:
+ paalped, rhettinger, terry.reedy, r.david.murray, serhiy.storchaka, eitan.adler |
2018-05-26 11:01:36 | paalped | link | issue33647 messages |
2018-05-26 11:01:36 | paalped | create | |
|