Thus spake Bart Cramer:

> I have a question... :
>
> >>> class A: pass
> ...
> >>> class B(object) : pass
> ...
> >>> dir(A)
> ['__doc__', '__module__']
> >>> dir(B)
> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
> '__hash__', '__init__', '__module__', '__new__', '__reduce__',
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']
>
> What is the difference between classes A and B? The funny thing is, when I
> put these things into an array of length, say, a million, it turns out
> that
> class A eats about 184 bytes per instantiation, and class B a lot less
> (sic!): plm 50 bytes. How come?
>

class B is a "new-style' class, meaning that it inherits from a base,
pre-existing class (in this case "object", which is as basic and generic as
you can get!).  class A has to start from nothing, which is why it consumes
more memory yet has less functionality.  (As a very, very rough analogy,
imagine that you have thirty seconds to paint a sign.  Which would be more
efficient - freehand or a stencil?  Given time, you can achieve more
artistic results freehand, but the stencil gets you quick results.)

As I understand it, in Python 3000 (aka 3.0), new-style classes will be
mandatory.  As you can already see, they give you more bang for the buck, so
you might as well get used to using them now!

-- 
www.fsrtechnologies.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to