Why less emphasis on private data?
Bart Ogryczak
B.Ogryczak at gmail.com
Mon Feb 5 10:22:00 EST 2007
On Jan 7, 1:07 am, "time.sw... at gmail.com" <time.sw... at gmail.com>
wrote:
> Coming from a C++ / C# background, the lack of emphasis on private data
> seems weird to me. I've often found wrapping private data useful to
> prevent bugs and enforce error checking..
>
> It appears to me (perhaps wrongly) that Python prefers to leave class
> data public. What is the logic behind that choice?
Often it´s a question of efficiency. Function calls in Python are
bloody slow. There is no "inline" directive, since it´s intepreted,
not compiled. Eg. consider code like that:
class MyWhatever:
...
def getSomeAttr(self):
return self._someAttr
def getSomeOtherAttr(self):
return self._someOtherAttr
[x.getSomeAttr() for x in listOfMyWhatevers if x.getSomeOtherAttr() ==
'whatever']
You´d get it running hundreds times faster doing it the "wrong" way:
[x._someAttr for x in listOfMyWhatevers if x._someOtherAttr ==
'whatever']
More information about the Python-list
mailing list