python class !! based@C struct

Java, c# and c++ classes are based on C struct, but Python class is more of a dictionary, or rather … A python class instance is more of a dictionary instance. (Cf javascript)

Telltale sign — You can instantiate a python class and subsequently add fields to it.

Even bolder, an instance myObj of class MyClass can introduce a new method m() that’s completely missing in MyClass. As a result, python run-time dispatch algo will search
1) the instance’s private registry
2) the class’s registry
3) base class’s registry

I think the registry is an internal dictionary (“idic”). As stated in P302 [[Programming Python]], classes are just dictionaries that inherit from other dictionaries; each instance is a new dictionary. All these run-time searches hit the dictionaries.

I feel python introspection is more sophisticated and more comprehensive than c++/c#/java.

Note this applies to method resolution only. Fields? Probably similar but i’m not sure yet.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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