Anonymous class question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Williams

    Anonymous class question

    Python experts,

    Is there a more pythonic way to do something evquilent to what this line
    does without creating a dummy class?

    self.file = type("", (object,), {'close':lambda slf: None})()

    As you can guess, I want a dummy object that I can call close on with
    impunity.

    I've been reading _Python in a Nutshell_ (Thanks, Alex!), which defines
    a Bunch class that I think could also work, but if I'm only going to use
    it to define a class with a dummy close() method, I might as well just
    create the dummy class with the dummy method. . . Unless there is
    something equvilent to Bunch in one of the standard modules that I don't
    know about . . .?

  • Carl Banks

    #2
    Re: Anonymous class question

    Dan Williams wrote:[color=blue]
    > Python experts,
    >
    > Is there a more pythonic way to do something evquilent to what this line
    > does without creating a dummy class?
    >
    > self.file = type("", (object,), {'close':lambda slf: None})()
    >
    > As you can guess, I want a dummy object that I can call close on with
    > impunity.[/color]

    The above line does create a dummy class. What's your motivation for
    doing this--what's wrong with defining a dummy class? Is there a
    reason you want it to be nameless? Do you want to do this simply to
    save typing? Do you have hundreds of these dummy classes and don't
    want to type a new class each time? The Pythonic way is to wrap it in
    a function:


    def nameless_dummy_ object_with_met hods(*methods):
    d = {}
    for sym in methods:
    d[sym] = lambda self,*args,**kw args: None
    return type("",(object ,),d)()


    self.file = nameless_dummy_ object_with_met hods('close')


    You specify any methods you want as strings and the functions builds a
    class with those methds, which quietly accept any arguments they are
    passed, and returns an instance of it. Presumably, the code works
    with regular objects, and trying to define the right "prototype" for
    each method is doing unnecessary work.


    --
    CARL BANKS
    "You don't run Microsoft Windows. Microsoft Windows runs you."

    Comment

    • Dan Williams

      #3
      Re: Anonymous class question

      Bengt Richter wrote:[color=blue]
      > On Thu, 07 Aug 2003 03:20:24 GMT, Carl Banks <imbosol@aerojo ckey.com> wrote:
      >
      >[color=green]
      >>Dan Williams wrote:
      >>[color=darkred]
      >>>Python experts,
      >>>
      >>>Is there a more pythonic way to do something evquilent to what this line
      >>>does without creating a dummy class?
      >>>
      >>>self.file = type("", (object,), {'close':lambda slf: None})()
      >>>[/color]
      >>[/color]
      > Does that (object,) do something I'm missing?
      >[color=green][color=darkred]
      > >>> o1 = type('',(object ,),{})()
      > >>> o2 = type('',(),{})( )
      > >>> type(o1).__base s__[/color][/color]
      > (<type 'object'>,)[color=green][color=darkred]
      > >>> type(o2).__base s__[/color][/color]
      > (<type 'object'>,)
      >
      > Regards,
      > Bengt Richter[/color]

      I thought it made it a new-style class. I could be wrong about that,
      though. . .

      -Dan

      Comment

      • Alex Martelli

        #4
        Re: Anonymous class question

        Dan Williams wrote:
        ...[color=blue][color=green][color=darkred]
        >>>>self.file = type("", (object,), {'close':lambda slf: None})()
        >>>[/color]
        >> Does that (object,) do something I'm missing?
        >>[color=darkred]
        >> >>> o1 = type('',(object ,),{})()
        >> >>> o2 = type('',(),{})( )
        >> >>> type(o1).__base s__[/color]
        >> (<type 'object'>,)[color=darkred]
        >> >>> type(o2).__base s__[/color]
        >> (<type 'object'>,)
        >>
        >> Regards,
        >> Bengt Richter[/color]
        >
        > I thought it made it a new-style class. I could be wrong about that,
        > though. . .[/color]

        Class whose metaclass is type (and it surely will be, if you
        instantiate type directly by calling it as you did) ARE "new
        style" by definition. Don't be confused with the class
        statement: _then_ you may need to explicitly specify object
        as a base class [or otherwise set the __metaclass__] in order
        to have a class be new-style rather than the default 'classic'.
        But when you explicitly call type, specifiying object as the
        only base is not necessary (although, it _is_ innocuous).


        Alex

        Comment

        Working...