A class for C-like structuures in Python

Donn Cave donn at u.washington.edu
Wed Aug 9 17:36:37 EDT 2000


Quoth Thomas Gagne <tgagne at ix.netcom.com>:
| I don't think I should be using __dict__, __getattr() and __setattr().  The
| problem is it seems to internalize the member list as though they were fields
| inside the structure.  This could be fine except that a) I can't get it
| working (see previous message) and b) what if the variables defined conflict
| with variables that are part of the class hierarchy?  Suppose someone wanted
| to name a variable __str__ for whatever reason, this could be bad in Python.
| But holding a dictionary entry for "__str__" wouldn't.
|
| Having to code IsdHeader.valueFor('len') instead of the shorter IsdHeader.len
| may be a tad awkward, but it should work in all circumstances, no?

I don't like __get/setattr__ a lot, but advantages come to mind:

1.  There is no API, so
  a) not a tad awkward, and
  b) can be installed when necessary, only after it's clear they're needed.
2.  Performance costs of accessors could be non-trivial, and with a
    combination of __getattr__ and __dict__ you can get some lookups
    free if the attributes are, at your discretion, defined directly
    in __dict__.

All of this reflects the point of view that accessor functions aren't
standard operating procedure in Python.  I can't speak for anyone but
myself, but that's my bias, I think they're usually out of place.  The
motive is good, but we get better return on time invested in higher
level functionality.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list