namespace dictionaries ok?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Adam

    namespace dictionaries ok?


    Hi, I found the following to be a useful way to access arguments after
    they are passed to a function that collects them with **kwds.


    class namespace(dict) :
    def __getattr__(sel f, name):
    return self.__getitem_ _(name)
    def __setattr__(sel f, name, value):
    self.__setitem_ _(name, value)
    def __delattr__(sel f, name):
    self.__delitem_ _(name)

    def foo(**kwds):
    kwds = namespace(kwds)
    print kwds.color, kwds.size, kwds.shape etc....

    foo( color='red', size='large', shape='ball', .... etc..)


    It just seems awkward to have to use "string keys" in this situation.
    This is easy and still retains the dictionary so it can be modified and
    passed to another function or method as kwds again.

    Any thoughts? Any better way to do this?

    Cheers, Ron

  • James Stroud

    #2
    Re: namespace dictionaries ok?

    Here it goes with a little less overhead:


    py> class namespace:
    .... def __init__(self, adict):
    .... self.__dict__.u pdate(adict)
    ....
    py> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
    py> n.bob
    1
    py> n.ted
    3

    James

    On Monday 24 October 2005 19:06, Ron Adam wrote:[color=blue]
    > Hi, I found the following to be a useful way to access arguments after
    > they are passed to a function that collects them with **kwds.
    >
    >
    > class namespace(dict) :
    > def __getattr__(sel f, name):
    > return self.__getitem_ _(name)
    > def __setattr__(sel f, name, value):
    > self.__setitem_ _(name, value)
    > def __delattr__(sel f, name):
    > self.__delitem_ _(name)
    >
    > def foo(**kwds):
    > kwds = namespace(kwds)
    > print kwds.color, kwds.size, kwds.shape etc....
    >
    > foo( color='red', size='large', shape='ball', .... etc..)
    >
    >
    > It just seems awkward to have to use "string keys" in this situation.
    > This is easy and still retains the dictionary so it can be modified and
    > passed to another function or method as kwds again.
    >
    > Any thoughts? Any better way to do this?
    >
    > Cheers, Ron[/color]

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • Simon Burton

      #3
      Re: namespace dictionaries ok?


      Yes!

      I do this a lot when i have deeply nested function calls
      a->b->c->d->e
      and need to pass args to the deep function without changing the
      middle functions.

      In this situation I think i would prefer this variation:

      class Context(dict):
      def __init__(self,* *kwds):
      dict.__init__(s elf,kwds)
      def __getattr__(sel f, name):
      return self.__getitem_ _(name)
      def __setattr__(sel f, name, value):
      self.__setitem_ _(name, value)
      def __delattr__(sel f, name):
      self.__delitem_ _(name)

      def foo(ctx):
      print ctx.color, ctx.size, ctx.shape

      foo( Context(color=' red', size='large', shape='ball') )


      This is looking like foo should be a method of Context now,
      but in my situation foo is already a method of another class.

      Simon.

      Comment

      • Ron Adam

        #4
        Re: namespace dictionaries ok?

        James Stroud wrote:[color=blue]
        > Here it goes with a little less overhead:
        >
        >
        > py> class namespace:
        > ... def __init__(self, adict):
        > ... self.__dict__.u pdate(adict)
        > ...
        > py> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
        > py> n.bob
        > 1
        > py> n.ted
        > 3
        >
        > James[/color]

        But it's not a dictionary anymore so you can't use it in the same places
        you would use a dictionary.

        foo(**n)

        Would raise an error.

        So I couldn't do:

        def foo(**kwds):
        kwds = namespace(kwds)
        kwds.bob = 3
        kwds.alice = 5
        ...
        bar(**kwds) #<--- do something with changed items

        Ron

        [color=blue]
        > On Monday 24 October 2005 19:06, Ron Adam wrote:
        >[color=green]
        >>Hi, I found the following to be a useful way to access arguments after
        >>they are passed to a function that collects them with **kwds.
        >>
        >>
        >> class namespace(dict) :
        >> def __getattr__(sel f, name):
        >> return self.__getitem_ _(name)
        >> def __setattr__(sel f, name, value):
        >> self.__setitem_ _(name, value)
        >> def __delattr__(sel f, name):
        >> self.__delitem_ _(name)
        >>
        >> def foo(**kwds):
        >> kwds = namespace(kwds)
        >> print kwds.color, kwds.size, kwds.shape etc....
        >>
        >> foo( color='red', size='large', shape='ball', .... etc..)
        >>
        >>
        >>It just seems awkward to have to use "string keys" in this situation.
        >>This is easy and still retains the dictionary so it can be modified and
        >>passed to another function or method as kwds again.
        >>
        >>Any thoughts? Any better way to do this?
        >>
        >>Cheers, Ron[/color][/color]

        Comment

        • Ron Adam

          #5
          Re: namespace dictionaries ok?

          Simon Burton wrote:
          [color=blue]
          > Yes!
          >
          > I do this a lot when i have deeply nested function calls
          > a->b->c->d->e
          > and need to pass args to the deep function without changing the
          > middle functions.[/color]

          Yes, :-) Which is something like what I'm doing also. Get the
          dictionary, modify it or validate it somehow, then pass it on. I also
          find that when I'm passing variables as keywords,

          foo(name=name, address=address , city=city)

          I really don't want (or like) to have to access the names with
          dictionary key as *strings* in the function that is called and collects
          them in a single object.

          [color=blue]
          > In this situation I think i would prefer this variation:
          >
          > class Context(dict):
          > def __init__(self,* *kwds):
          > dict.__init__(s elf,kwds)
          > def __getattr__(sel f, name):
          > return self.__getitem_ _(name)
          > def __setattr__(sel f, name, value):
          > self.__setitem_ _(name, value)
          > def __delattr__(sel f, name):
          > self.__delitem_ _(name)
          >
          > def foo(ctx):
          > print ctx.color, ctx.size, ctx.shape
          >
          > foo( Context(color=' red', size='large', shape='ball') )
          >
          >
          > This is looking like foo should be a method of Context now,
          > but in my situation foo is already a method of another class.
          >
          > Simon.[/color]

          I didn't see what you were referring to at first. But yes, I see the
          similarity.

          Cheers,
          Ron






          Comment

          • James Stroud

            #6
            Re: namespace dictionaries ok?

            Oops. Answered before I finished reading the question.

            James

            On Monday 24 October 2005 19:53, Ron Adam wrote:[color=blue]
            > James Stroud wrote:[color=green]
            > > Here it goes with a little less overhead:
            > >
            > >
            > > py> class namespace:
            > > ... def __init__(self, adict):
            > > ... self.__dict__.u pdate(adict)
            > > ...
            > > py> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
            > > py> n.bob
            > > 1
            > > py> n.ted
            > > 3
            > >
            > > James[/color]
            >
            > But it's not a dictionary anymore so you can't use it in the same places
            > you would use a dictionary.
            >
            > foo(**n)
            >
            > Would raise an error.
            >
            > So I couldn't do:
            >
            > def foo(**kwds):
            > kwds = namespace(kwds)
            > kwds.bob = 3
            > kwds.alice = 5
            > ...
            > bar(**kwds) #<--- do something with changed items
            >
            > Ron
            >[color=green]
            > > On Monday 24 October 2005 19:06, Ron Adam wrote:[color=darkred]
            > >>Hi, I found the following to be a useful way to access arguments after
            > >>they are passed to a function that collects them with **kwds.
            > >>
            > >>
            > >> class namespace(dict) :
            > >> def __getattr__(sel f, name):
            > >> return self.__getitem_ _(name)
            > >> def __setattr__(sel f, name, value):
            > >> self.__setitem_ _(name, value)
            > >> def __delattr__(sel f, name):
            > >> self.__delitem_ _(name)
            > >>
            > >> def foo(**kwds):
            > >> kwds = namespace(kwds)
            > >> print kwds.color, kwds.size, kwds.shape etc....
            > >>
            > >> foo( color='red', size='large', shape='ball', .... etc..)
            > >>
            > >>
            > >>It just seems awkward to have to use "string keys" in this situation.
            > >>This is easy and still retains the dictionary so it can be modified and
            > >>passed to another function or method as kwds again.
            > >>
            > >>Any thoughts? Any better way to do this?
            > >>
            > >>Cheers, Ron[/color][/color][/color]

            --
            James Stroud
            UCLA-DOE Institute for Genomics and Proteomics
            Box 951570
            Los Angeles, CA 90095


            Comment

            • Bengt Richter

              #7
              Re: namespace dictionaries ok?

              On Tue, 25 Oct 2005 03:10:17 GMT, Ron Adam <rrr@ronadam.co m> wrote:
              [color=blue]
              >Simon Burton wrote:
              >[color=green]
              >> Yes!
              >>
              >> I do this a lot when i have deeply nested function calls
              >> a->b->c->d->e
              >> and need to pass args to the deep function without changing the
              >> middle functions.[/color]
              >
              >Yes, :-) Which is something like what I'm doing also. Get the
              >dictionary, modify it or validate it somehow, then pass it on. I also
              >find that when I'm passing variables as keywords,
              >
              > foo(name=name, address=address , city=city)
              >
              >I really don't want (or like) to have to access the names with
              >dictionary key as *strings* in the function that is called and collects
              >them in a single object.
              >
              >[color=green]
              >> In this situation I think i would prefer this variation:
              >>
              >> class Context(dict):
              >> def __init__(self,* *kwds):
              >> dict.__init__(s elf,kwds)
              >> def __getattr__(sel f, name):
              >> return self.__getitem_ _(name)
              >> def __setattr__(sel f, name, value):
              >> self.__setitem_ _(name, value)
              >> def __delattr__(sel f, name):
              >> self.__delitem_ _(name)
              > >
              >> def foo(ctx):
              >> print ctx.color, ctx.size, ctx.shape
              >>
              >> foo( Context(color=' red', size='large', shape='ball') )
              >>
              >>
              >> This is looking like foo should be a method of Context now,
              >> but in my situation foo is already a method of another class.
              >>[/color][/color]
              Or maybe just add a __repr__ method, if you want to see a readable
              representation (e.g., see below).[color=blue][color=green]
              >> Simon.[/color]
              >
              >I didn't see what you were referring to at first. But yes, I see the
              >similarity.
              >[color=green][color=darkred]
              >>> class Context(dict):[/color][/color][/color]
              ... def __init__(self,* *kwds):
              ... dict.__init__(s elf,kwds)
              ... def __getattr__(sel f, name):
              ... return self.__getitem_ _(name)
              ... def __setattr__(sel f, name, value):
              ... self.__setitem_ _(name, value)
              ... def __delattr__(sel f, name):
              ... self.__delitem_ _(name)
              ... def __repr__(self):
              ... return 'Context(%s)' % ', '.join('%s=%r'% t for t in sorted(self.ite ms()))
              ...[color=blue][color=green][color=darkred]
              >>> print Context(color=' red', size='large', shape='ball')[/color][/color][/color]
              Context(color=' red', shape='ball', size='large')[color=blue][color=green][color=darkred]
              >>> ctx = Context(color=' red', size='large', shape='ball')
              >>> print ctx[/color][/color][/color]
              Context(color=' red', shape='ball', size='large')[color=blue][color=green][color=darkred]
              >>> ctx[/color][/color][/color]
              Context(color=' red', shape='ball', size='large')[color=blue][color=green][color=darkred]
              >>> ctx.color[/color][/color][/color]
              'red'

              Regards,
              Bengt Richter

              Comment

              • Ron Adam

                #8
                Re: namespace dictionaries ok?

                James Stroud wrote:[color=blue]
                > Oops. Answered before I finished reading the question.
                >
                > James[/color]

                Well, the one bad side effect (or feature depending on the
                circumstance), is it makes a copy. I wonder if there is a way to modify
                the dictionary in place with a function to do the same thing instead of
                creating a new object?

                Cheers,
                Ron

                [color=blue]
                > On Monday 24 October 2005 19:53, Ron Adam wrote:
                >[color=green]
                >>James Stroud wrote:
                >>[color=darkred]
                >>>Here it goes with a little less overhead:
                >>>
                >>>
                >>>py> class namespace:
                >>>... def __init__(self, adict):
                >>>... self.__dict__.u pdate(adict)
                >>>...
                >>>py> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
                >>>py> n.bob
                >>>1
                >>>py> n.ted
                >>>3
                >>>
                >>>James[/color]
                >>
                >>But it's not a dictionary anymore so you can't use it in the same places
                >>you would use a dictionary.
                >>
                >> foo(**n)
                >>
                >>Would raise an error.
                >>
                >>So I couldn't do:
                >>
                >> def foo(**kwds):
                >> kwds = namespace(kwds)
                >> kwds.bob = 3
                >> kwds.alice = 5
                >> ...
                >> bar(**kwds) #<--- do something with changed items
                >>
                >>Ron
                >>
                >>[color=darkred]
                >>>On Monday 24 October 2005 19:06, Ron Adam wrote:
                >>>
                >>>>Hi, I found the following to be a useful way to access arguments after
                >>>>they are passed to a function that collects them with **kwds.
                >>>>
                >>>>
                >>>> class namespace(dict) :
                >>>> def __getattr__(sel f, name):
                >>>> return self.__getitem_ _(name)
                >>>> def __setattr__(sel f, name, value):
                >>>> self.__setitem_ _(name, value)
                >>>> def __delattr__(sel f, name):
                >>>> self.__delitem_ _(name)
                >>>>
                >>>> def foo(**kwds):
                >>>> kwds = namespace(kwds)
                >>>> print kwds.color, kwds.size, kwds.shape etc....
                >>>>
                >>>> foo( color='red', size='large', shape='ball', .... etc..)
                >>>>
                >>>>
                >>>>It just seems awkward to have to use "string keys" in this situation.
                >>>>This is easy and still retains the dictionary so it can be modified and
                >>>>passed to another function or method as kwds again.
                >>>>
                >>>>Any thoughts? Any better way to do this?
                >>>>
                >>>>Cheers, Ron[/color][/color]
                >
                >[/color]

                Comment

                • Ron Adam

                  #9
                  Re: namespace dictionaries ok?

                  James Stroud wrote:[color=blue]
                  > Here it goes with a little less overhead:
                  >
                  >
                  > py> class namespace:
                  > ... def __init__(self, adict):
                  > ... self.__dict__.u pdate(adict)
                  > ...
                  > py> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
                  > py> n.bob
                  > 1
                  > py> n.ted
                  > 3
                  >
                  > James[/color]

                  How about...

                  class namespace(dict) :
                  __getattr__ = dict.__getitem_ _
                  __setattr__ = dict.__setitem_ _
                  __delattr__ = dict.__delitem_ _


                  This seems to work, and eliminates the indirect method calls.

                  Cheers,
                  Ron

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: namespace dictionaries ok?

                    Simon Burton wrote:

                    [color=blue]
                    > Yes!
                    >
                    > I do this a lot when i have deeply nested function calls
                    > a->b->c->d->e
                    > and need to pass args to the deep function without changing the
                    > middle functions.[/color]

                    If you find yourself passing arguments to functions
                    just so they can in turn pass them on to other
                    functions, you probably shouldn't be using deeply
                    nested function calls and should be looking for another
                    program structure.


                    --
                    Steven.

                    Comment

                    • Duncan Booth

                      #11
                      Re: namespace dictionaries ok?

                      Ron Adam wrote:[color=blue]
                      > James Stroud wrote:[color=green]
                      >> Here it goes with a little less overhead:
                      >>
                      >>[/color][/color]
                      <example snipped>[color=blue]
                      >
                      > But it's not a dictionary anymore so you can't use it in the same places
                      > you would use a dictionary.
                      >
                      > foo(**n)
                      >
                      > Would raise an error.
                      >
                      > So I couldn't do:
                      >
                      > def foo(**kwds):
                      > kwds = namespace(kwds)
                      > kwds.bob = 3
                      > kwds.alice = 5
                      > ...
                      > bar(**kwds) #<--- do something with changed items
                      >[/color]
                      I agree with Steven D'Aprano's reply, but if you are set on it you could
                      try this:
                      [color=blue][color=green][color=darkred]
                      >>> class namespace(dict) :[/color][/color][/color]
                      def __init__(self, *args, **kw):
                      dict.__init__(s elf, *args, **kw)
                      self.__dict__ = self

                      [color=blue][color=green][color=darkred]
                      >>> n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
                      >>> n.bob[/color][/color][/color]
                      1[color=blue][color=green][color=darkred]
                      >>> n.bob = 3
                      >>> n['bob'][/color][/color][/color]
                      3

                      The big problem of course with this sort of approach is that you cannot
                      then safely use any of the methods of the underlying dict as they could be
                      masked by values.

                      P.S. James, *please* could you avoid top-quoting.

                      Comment

                      • Ron Adam

                        #12
                        Re: namespace dictionaries ok?

                        Duncan Booth wrote:[color=blue]
                        > Ron Adam wrote:
                        >[color=green]
                        >>James Stroud wrote:
                        >>[color=darkred]
                        >>>Here it goes with a little less overhead:
                        >>>
                        >>>[/color][/color]
                        >
                        > <example snipped>
                        >[color=green]
                        >>But it's not a dictionary anymore so you can't use it in the same places
                        >>you would use a dictionary.
                        >>
                        >> foo(**n)
                        >>
                        >>Would raise an error.
                        >>
                        >>So I couldn't do:
                        >>
                        >> def foo(**kwds):
                        >> kwds = namespace(kwds)
                        >> kwds.bob = 3
                        >> kwds.alice = 5
                        >> ...
                        >> bar(**kwds) #<--- do something with changed items
                        >>[/color]
                        >
                        > I agree with Steven D'Aprano's reply, but if you are set on it you could
                        > try this:
                        >
                        >[color=green][color=darkred]
                        >>>>class namespace(dict) :[/color][/color]
                        >
                        > def __init__(self, *args, **kw):
                        > dict.__init__(s elf, *args, **kw)
                        > self.__dict__ = self
                        >
                        >
                        >[color=green][color=darkred]
                        >>>>n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
                        >>>>n.bob[/color][/color]
                        >
                        > 1
                        >[color=green][color=darkred]
                        >>>>n.bob = 3
                        >>>>n['bob'][/color][/color]
                        >
                        > 3
                        >
                        > The big problem of course with this sort of approach is that you cannot
                        > then safely use any of the methods of the underlying dict as they could be
                        > masked by values.
                        >
                        > P.S. James, *please* could you avoid top-quoting.[/color]

                        Or worse, the dictionary would become not functional depending on what
                        methods were masked.


                        And this approach reverses that, The dict values will be masked by the
                        methods, so the values can't effect the dictionary methods. But those
                        specific values are only retrievable with the standard dictionary notation.

                        class namespace(dict) :
                        __getattr__ = dict.__getitem_ _
                        __setattr__ = dict.__setitem_ _
                        __delattr__ = dict.__delitem_ _

                        n = namespace()
                        n.__getattr__ = 'yes' # doesn't mask __getattr__ method.

                        print n['__getattr__'] -> 'yes'

                        The value is there and __getattr__() still works. But n.__getattr__
                        returns the method not the value.

                        So is there a way to keep the functionality without loosing the methods?


                        BTW, I agree with Steven concerning data structures. This really isn't
                        a substitute for a data structure. Many keys will not work with this.

                        n.my name = 'Ron'
                        n.(1,2) = 25
                        n.John's = [ ... ]

                        The use case I'm thinking of is not as a shortcut for data structures,
                        but instead, as a way to keep names as names, and maintaining those
                        names in a group. Thus the namespace association.

                        def foo(**kwds):
                        kwds = namespace(kwds)
                        print kwds.name
                        print kwds.value
                        ...

                        name = 'ron'
                        value = 25
                        foo( name=name, position=positi on )

                        Cheers,
                        Ron

                        Comment

                        • James Stroud

                          #13
                          Top-quoting defined [was: namespace dictionaries ok?]

                          On Tuesday 25 October 2005 00:31, Duncan Booth wrote:[color=blue]
                          > P.S. James, *please* could you avoid top-quoting[/color]

                          Were it not for Steve Holden's providing me with a link off the list, I would
                          have never known to what it is you are referring. I have read some relevant
                          literature to find that this is more widely known as "top-posting". I'll go
                          with majority rules here, but I would like to say that my lack of
                          "netiquette " in this matter comes from practicality and not malice. I have
                          read many a letter both in-line quoted and/or top-posted and have never
                          really made a distinction between the two. Both styles have been very easy
                          for me to follow in the past using my mouse wheel (first added to the mouse
                          device circa 1995, when a 486/DX33 was still considered a powerful machine).
                          I'm sorry if I can't find a dumb terminal for a "VAX" with which to read my
                          email. Perhaps, if i could, I would understand your frustration a little
                          better.

                          The only reason I'm making a point of this is that many people come to python
                          from fields other than computer science or hacker-ology. Posting styles will
                          vary considerably among these people, so deal with the deviants carefully.
                          Their differences in styles, like mine, probably arise from the culture of
                          their respective fields. Most, like me, may not even know what the heck you
                          are talking about. Also, here is a well written synopsis of the arguments in
                          favor of top-posting and they may even be strong enough to legitimize the
                          practice:



                          In light of these arguments, I hereby reserve the right to revert to
                          top-posting if the compulsion overwhelms me.

                          James

                          --
                          James Stroud
                          UCLA-DOE Institute for Genomics and Proteomics
                          Box 951570
                          Los Angeles, CA 90095


                          Comment

                          • Bengt Richter

                            #14
                            Re: namespace dictionaries ok?

                            On Tue, 25 Oct 2005 16:20:21 GMT, Ron Adam <rrr@ronadam.co m> wrote:
                            [color=blue]
                            >Duncan Booth wrote:[color=green]
                            >> Ron Adam wrote:
                            >>[color=darkred]
                            >>>James Stroud wrote:
                            >>>
                            >>>>Here it goes with a little less overhead:
                            >>>>
                            >>>>[/color]
                            >>
                            >> <example snipped>
                            >>[color=darkred]
                            >>>But it's not a dictionary anymore so you can't use it in the same places
                            >>>you would use a dictionary.
                            >>>
                            >>> foo(**n)
                            >>>
                            >>>Would raise an error.
                            >>>
                            >>>So I couldn't do:
                            >>>
                            >>> def foo(**kwds):
                            >>> kwds = namespace(kwds)
                            >>> kwds.bob = 3
                            >>> kwds.alice = 5
                            >>> ...
                            >>> bar(**kwds) #<--- do something with changed items
                            >>>[/color]
                            >>
                            >> I agree with Steven D'Aprano's reply, but if you are set on it you could
                            >> try this:
                            >>
                            >>[color=darkred]
                            >>>>>class namespace(dict) :[/color]
                            >>
                            >> def __init__(self, *args, **kw):
                            >> dict.__init__(s elf, *args, **kw)
                            >> self.__dict__ = self
                            >>
                            >>
                            >>[color=darkred]
                            >>>>>n = namespace({'bob ':1, 'carol':2, 'ted':3, 'alice':4})
                            >>>>>n.bob[/color]
                            >>
                            >> 1
                            >>[color=darkred]
                            >>>>>n.bob = 3
                            >>>>>n['bob'][/color]
                            >>
                            >> 3
                            >>
                            >> The big problem of course with this sort of approach is that you cannot
                            >> then safely use any of the methods of the underlying dict as they could be
                            >> masked by values.
                            >>
                            >> P.S. James, *please* could you avoid top-quoting.[/color]
                            >
                            >Or worse, the dictionary would become not functional depending on what
                            >methods were masked.
                            >
                            >
                            >And this approach reverses that, The dict values will be masked by the
                            >methods, so the values can't effect the dictionary methods. But those
                            >specific values are only retrievable with the standard dictionary notation.
                            >
                            > class namespace(dict) :
                            > __getattr__ = dict.__getitem_ _
                            > __setattr__ = dict.__setitem_ _
                            > __delattr__ = dict.__delitem_ _
                            >
                            > n = namespace()
                            > n.__getattr__ = 'yes' # doesn't mask __getattr__ method.
                            >
                            > print n['__getattr__'] -> 'yes'
                            >
                            >The value is there and __getattr__() still works. But n.__getattr__
                            >returns the method not the value.
                            >
                            >So is there a way to keep the functionality without loosing the methods?
                            >
                            >
                            >BTW, I agree with Steven concerning data structures. This really isn't
                            >a substitute for a data structure. Many keys will not work with this.
                            >
                            > n.my name = 'Ron'
                            > n.(1,2) = 25
                            > n.John's = [ ... ]
                            >
                            >The use case I'm thinking of is not as a shortcut for data structures,
                            >but instead, as a way to keep names as names, and maintaining those
                            >names in a group. Thus the namespace association.
                            >
                            > def foo(**kwds):
                            > kwds = namespace(kwds)
                            > print kwds.name
                            > print kwds.value
                            > ...
                            >
                            > name = 'ron'
                            > value = 25
                            > foo( name=name, position=positi on )
                            >[/color]
                            Just had the thought that if you want to add bindings on the fly modifying the
                            original object, you could use the __call__ method, e.g.,
                            [color=blue][color=green][color=darkred]
                            >>> class NameSpace(dict) :[/color][/color][/color]
                            ... __getattr__ = dict.__getitem_ _
                            ... __setattr__ = dict.__setitem_ _
                            ... __delattr__ = dict.__delitem_ _
                            ... def __call__(self, **upd):
                            ... self.update(upd )
                            ... return self
                            ...[color=blue][color=green][color=darkred]
                            >>> def show(x): print '-- showing %r'%x; return x[/color][/color][/color]
                            ...[color=blue][color=green][color=darkred]
                            >>> ns = NameSpace(initi al=1)
                            >>> show(ns)[/color][/color][/color]
                            -- showing {'initial': 1}
                            {'initial': 1}

                            And updating with a second keyword on the fly:
                            [color=blue][color=green][color=darkred]
                            >>> show(show(ns)(s econd=2))[/color][/color][/color]
                            -- showing {'initial': 1}
                            -- showing {'second': 2, 'initial': 1}
                            {'second': 2, 'initial': 1}

                            FWIW ;-)

                            Regards,
                            Bengt Richter

                            Comment

                            • Mike Meyer

                              #15
                              Re: Top-quoting defined [was: namespace dictionaries ok?]

                              > On Tuesday 25 October 2005 00:31, Duncan Booth wrote:[color=blue][color=green]
                              >> P.S. James, *please* could you avoid top-quoting[/color]
                              > James Stroud <jstroud@mbi.uc la.edu> writes:
                              > I'm sorry if I can't find a dumb terminal for a "VAX" with which to read my
                              > email. Perhaps, if i could, I would understand your frustration a little
                              > better.[/color]

                              You don't need a VAX. All it takes is trying to dethread a top-posted
                              conversation. I've given up in frustration on more than one such
                              venture.
                              [color=blue]
                              > The only reason I'm making a point of this is that many people come to python> from fields other than computer science or hacker-ology. Posting styles will
                              > vary considerably among these people, so deal with the deviants carefully.[/color]

                              I belive Duncan did so. He asked you to "please* avoid to
                              top-posting".
                              [color=blue]
                              > Their differences in styles, like mine, probably arise from the culture of
                              > their respective fields. Most, like me, may not even know what the heck you
                              > are talking about. Also, here is a well written synopsis of the arguments in
                              > favor of top-posting and they may even be strong enough to legitimize the
                              > practice:
                              > http://alpage.ath.cx/toppost/toppost.htm[/color]

                              Well, if I could find a 300 baud modem, I might understand his
                              objection to not top-posting (he really only has one). His comments
                              about why people shouldn't complain about top-posting are full of
                              suggestions that don't generally work on netnews and mail, though they
                              may be valid elsewhere, and his suggestion about when to top-post
                              leaves out the *very important* criteria that you don't expect to read
                              any replies to your comment, and don't care if you make life difficult
                              for those who have different expectations.

                              He's right about inadequately-trimmed repklies, though. Not being on a
                              300 baud modem, I don't think they're as annoying as top-posting, but
                              they are certainly something that we can do without.
                              [color=blue]
                              > In light of these arguments, I hereby reserve the right to revert to
                              > top-posting if the compulsion overwhelms me.[/color]

                              That's your right. Be aware that people will ignore, correct and/or
                              complain about you doing so.

                              <mike
                              --
                              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                              Comment

                              Working...