How do I alias member functions in Python? 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?
No problemo! You can alias a method, but you have to know how to use it:

&gt;&gt;&gt; r=str.replace
&gt;&gt;&gt; a='hello'
&gt;&gt;&gt; r(r(r(r(a,'h','j'),'e','i'),'l','m'),'o','y')
'jimmy'


The key is that you have to pass self explicitly, because the alias is a kind of function that takes an extra argument that takes self:

&gt;&gt;&gt; type(r)
&lt;type 'method_descriptor'&gt;