In Python, one can save bytes by aliasing functions that are used repeatedly. For example:
r=range
a=r(100)
b=r(200)
c=r(300)
However, when the functions are member functions together, I don't know how to alias them in a way that allows chaining. For example:
s='Hello'
// Plain code
s=s.replace('H','J').replace('e','i').replace('l','m').replace('o','y')
// What I am trying to do
q=replace
s=s.q('H','J').q('e','i').q('l','m').q('o','y')
Obviously, what I am trying to do is not valid. And neither is this:
q=s.replace
s=q('H','J') // Replaces the 'H' in 'Hello'
s=q('e','i') // Replaces the 'e' in 'Hello'... and the J is gone.
s=q('l','m')
s=q('o','y')
Is there a another way to alias member functions and chained functions that saves characters?
q
meaning whatreplace
means in the class your using. \$\endgroup\$q
is bound to the replace method of that specificstr
instance. Also, remember you can do single char replacements with"Hello".replace(*"HJ")
\$\endgroup\$