On 7/24/09 6:23 PM, george hu wrote:
> In the chapter of "Add authorization",  we create a root factory in
> the model.py,
>
> class RootFactory(object):
>      __acl__ = [ (Allow, Everyone, 'view'), (Allow, 'editor', 'edit') ]
>      def __init__(self, environ):
>          self.__dict__.update(environ['bfg.routes.matchdict'])
>
>
> My question is the constructor here, I don't understand why there is a
> need to update the environ key of matchdict. I tried to print the the
> value before and after the update, they are just the same.

It's not updating the matchdict, it's updating the __dict__ of the RootFactory 
with the values in matchdict.

> And I've
> also tried to change the line to pass, the program worked without any
> issue. I don't quite understand the purpose of this code here.

It's not strictly necessary.  But if matchdict is {'foo':'bar'}, it makes it 
possible to do things like:

request.context.foo

... in view code.

To get a better sense of what's going on, fire up any Python interpreter and 
follow along with this:

[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> class Foo(object): pass
...
 >>> foo = Foo()
 >>> foo.__dict__
{}
 >>> foo.bar
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'Foo' object has no attribute 'bar'
 >>> foo.__dict__['bar'] = 1
 >>> foo.bar
1
 >>> newdict = {'a':10, 'b':12}
 >>> foo.__dict__.update(newdict)
 >>> foo.a
10
 >>> foo.b
12
 >>> foo.__dict__
{'a': 10, 'b': 12, 'bar': 1}
 >>>
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to