Steven Bethard wrote:
Nick Coghlan wrote:
I think the idea definitely deserves mention as a possible implementation strategy in the generic objects PEP, with the data argument made optional:


That's basically what the current implementation does (although I use 'update' instead of '='). The code is complicated because the implementation also takes all the argument types that dicts take.

The main difference I noticed is that by using update, any changes made via the attribute view are not reflected in the original dict.


By assigning to __dict__ directly, you can use the attribute view either as it's own dictionary (by not supplying one, or supplying a new one), or as a convenient way to programmatically modify an existing one. For example, you could use it to easily bind globals without needing the 'global' keyword:

Py> class attr_view(object):
...     def __init__(self, data):
...         self.__dict__ = data
...
Py> def f():
...   gbls = attr_view(globals())
...   gbls.x = 5
...
Py> x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
Py> f()
Py> x
5

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
            http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to