[Python-ideas] anonymous object support

Herman Sheremetyev herman at swebpage.com
Mon Jul 25 05:03:04 CEST 2011


On Mon, Jul 25, 2011 at 1:03 AM, Guido van Rossum <guido at python.org> wrote:
> On Sun, Jul 24, 2011 at 8:35 AM, Herman Sheremetyev <herman at swebpage.com> wrote:
>> On Mon, Jul 25, 2011 at 12:06 AM, Guido van Rossum <guido at python.org> wrote:
>>> On Sun, Jul 24, 2011 at 7:04 AM, Herman Sheremetyev <herman at swebpage.com> wrote:
>>>> It is currently somewhat difficult/awkward to create arbitrary
>>>> "anonymous" objects in Python.
>>>
>>> It looks like you are trying to duplicate Java's anonymous classes.
>>> Please don't try to emulate Java's work-arounds for Java's
>>> deficiencies.
>>
>> Ouch, and I don't even know Java well enough to deserve that ;)
>
> Ah, sorry.
>
>> I think a more accurate characterization would be that I'm trying to
>> emulate Javascript's object literal notation, but I was trying to
>> avoid parallels with other languages in my proposal.
>
> Still, IMO Javascript's objects suffer from a fatal confusion between
> being dicts and objects, so I still "object" to the idea. :-)
>
>> FWIW, I think it would be fine if this was a convenience function in
>> the standard library as suggested above. But it seems so basic that
>> having it supported in in the object() constructor would be the most
>> natural place rather than tucking it away in a library.
>
> Python's object is supposed to have only the minimal functionality.
> Its instances don't even have a __dict__.

What if there was a base "dummy" class that would allow this kind of
behavior as Steven suggested? My choice of object was simply because
it seems like the logical choice among the builtins and to get a
discussion going about the idea of creating objects as an expression
rather than statement, without resorting to defining a new class just
for that purpose.

For a short list of libraries that do this sort of thing (not all of
them support expression syntax) take a look at the "Simple fake
object" section in:

http://garybernhardt.github.com/python-mock-comparison/

Disclaimer: I'm the author of the flexmock library listed there.

There are also probably another half dozen libraries no longer being
maintained that are not listed there but have similar functionality
that can be found here:

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy#MockTestingTools

As well as a couple of new ones, like Chai, that aren't listed in the
testing taxonomy but solve the same problem.

Cheers,

-Herman



More information about the Python-ideas mailing list