Anonymous class question

Michael Chermside mcherm at mcherm.com
Thu Aug 7 07:50:00 EDT 2003


Dan Williams writes:
> 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})()

Dan, NO solution other than this one:

    class CanBeFreelyClosed(object):
        def close(self):
            pass
        [...]
    self.file = CanBeFreelyClosed

can be said to be "pythonic". In this case, there is one (obvious)
way to do it. Why on earth would you be afraid to create a class?

-- Michael Chermside






More information about the Python-list mailing list