python operator are wrapper over method

Java/c# operators are the basis of (and faster than) methods, but Python turns the stack upside down — some of the most common, workhorse operators are based on special methods —

dot —- __getattr__()
[key] i.e. key access —- __getitem__()
for-loop —- depends on more than one special method
the plus sign (“+”) —— __add__()
assignment? See other post

In terms of run-time performance, these efficient-looking operators are just as slow as a method call.

Q: I guess in python even the basic “+” cannot be a plain vanilla addition for simple types like integer. Why?

%%A: the basic addition operator should/ought-to change object state, like editing a spreadsheet cell in-place, but python integer addition creates a new Integer object. Therefore, I guess the Integer class must customize __add__() method

%%A: variable of a java primitive int type, c# simple int type or c++ nonref int type (not a ptr or reference) can have the basic addition operator, but python has no nonref type variable. All python int variables are pointers to Integer instances.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s