[albatross-users] impedance mismatch 1

Dave Cole djc at object-craft.com.au
Thu Jul 3 21:24:01 EST 2003


> OK, I think I understand what you mean about locals now and where
> your confusion comes from. locals is simply an empty class
> definition:
> 
>     class Vars:
>         pass
>     ...
>     ctx.locals = Vars()
> 
> Python allows you to set any attribute on any object at any time:
> 
>     v = Vars()
>     v.my_name = 'Matt'
> 
> This is perfectly valid and fairly normal in Python. After all, you
> don't have to define instance attributes, you just say "self.my_name
> = 'Matt'".

Excellent explanation.

> This reminds me that I have a note in a text file somewhere that
> says something like, "could/should ctx.locals behave like a
> dictionary?".

I suppose there is no reason why it shouldn't.  

> I think ctx.locals could be made to behave like a dictionary. An
> initial (untested) implementation of Vars to support this might be:
> 
>     class Vars:
>         def __setitem__(self, name, value):
>             setattr(self, name, value)
>         def __getitem__(self, name):
>             return getattr(self, name)

That is all you really need to do.

> There are other methods that would need implementing for Vars to be
> truly dictionary-like but I'm sure you get the idea.

If you really wanted to use things like .items() and .keys() you could
just do it on the .__dict__ member.

> Eric, would making ctx.locals behave like a dictionary help your
> understanding? Dave/Andrew, are there any technical reasons why this
> would not work?

None that I know of.  Is there any reason why you would want to
though.  I am not sure that you want to encourage people to mix code
like ctx.locals['a'] = 3 and ctx.locals.b = 5.

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list