A Suggestion for Python Dictionary/Class

billtj at my-deja.com billtj at my-deja.com
Wed Dec 27 09:55:44 EST 2000


Thanks for all the suggestions in making the Python class more like
C/C++ struct or class.  However, it looks that the current solution is
based on a "convention" to derive Python classes from a certain class.

I am not too familiar with how the Python interpreter works.  But based
on my current understanding, the current Python class is implemented
using dictionary, unlike in C/C++.  Therefore Python's

    var_1.member_1.member_2.member_3

will invoke three dictionary lookups, while C/C++'s

    var_1.member_1.member_2.member_3

is a (short) constant time operation, after compilation, of course.

My question is whether by defining a special class (call is struct?) in
Python will result in better Python run-time performance.  For example,

    struct MyStruct:
        def __init__ (self):
            self.member_1
            self.member_2
            self.member_3

Because it is a "struct" instead of a "class", we cannot add any new
member.  Can then the Python interpreter be optimized to handle struct's
so that the run-time performance is better than a class?  Coming from
C/C++ background and never saw the Python C code, I imagine that the
interpreter allocates memory space for three labels (which are
pointers/handles to Python objects?) and during
interpretation/compilation, Python replaces the references for member_1,
member_2, and member_3 simply to the pointers to the corresponding
labels.  (I may not be precise in describing this, but hopefully you get
the idea).  So no dictionary lookup is involved.

Any comments?

Bill


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list