Building CPython
Marko Rauhamaa
marko at pacujo.net
Sat May 16 04:08:25 EDT 2015
BartC <bc at freeuk.com>:
> I suppose in many cases an object will have no attributes of its own,
> and so it can rapidly bypass the first lookup.
Almost all objects have quite many instance attributes. That's what
tells objects apart.
> I don't understand the need for an object creation (to represent A.B
> so that it can call it?) but perhaps such an object can already exist,
> prepared ready for use.
Note that almost identical semantics could be achieved without a class.
Thus, these two constructs are almost identical:
class C:
def __init__(self, x):
self.x = x
def square(self):
return self.x * self.x
def cube(self):
return self.x * self.square()
##
class O: pass
def C(x):
o = O()
def square():
return x * x
def cube():
return x * square()
o.square = square
o.cube = cube
return o
IOW, the class is a virtually superfluous concept in Python. Python has
gotten it probably without much thought (other languages at the time had
it). I comes with advantages and disadvantages:
+ improves readability
+ makes objects slightly smaller
+ makes object instantiation slightly faster
- goes against the grain of ducktyping
- makes method calls slower
- makes method call semantics a bit tricky
Marko
More information about the Python-list
mailing list