[Python-ideas] anonymous object support

Paul Moore p.f.moore at gmail.com
Mon Jul 25 14:18:20 CEST 2011


On 25 July 2011 10:46, Herman Sheremetyev <herman at swebpage.com> wrote:
>> Can you point at any real code that implements a class-making function
>> such as you describe, and then uses it? I'd be curious to see any
>> real-world examples to check if my intuition that such code is
>> difficult to understand holds up.
>
> https://github.com/has207/flexmock/blob/master/tests/flexmock_test.py
>
> You can take a look at the tests starting at line 56 -- the flexmock()
> function does exactly what I'm proposing when given keyword args.

Thanks. That code does look nice and clean, although it is once again
using artificially simple attributes (although that's not surprising
in test code).

But I'm still puzzled as to what you're asking for. Clearly (I assume)
you're not asking for flexmock to be added to the stdlib. So are you
asking for something like flexmock - in which case, what are the use
cases for it (that aren't use cases for flexmock itself)?

Sorry if I'm being dense here. If others understand the request feel
free to drop this conversation as I'm not sure I'm adding much that I
haven't already said.

>From my point of view, I see 3 separate aspects:

1. Specialised functions like flexmock, which fill a particular niche,
and which quite reasonably hide the complexity of creating "anonymous"
objects behind a simple API. No problem here, these are clearly
useful, but they don't need to go into the stdlib unless their
particular focus warrants it.

2. A generic "create object with dynamically assigned attributes"
function. I haven't seen any use cases for this which aren't rare
enough that a multi-line answer, or Nick's type() workaround, aren't
sufficient.

3. Some functionality to make writing flexmock-like functions a bit
easier. Looking at the implementation of flexmock (specifically the
Mock class) I don't see any obvious place it would make sense to use
such a thing. You couldn't use type() to simplify Mock.__init__ as far
as I can see, and clearly type() is far too lightweight to replace
Mock completely. So again, I see no clear use case here.

But if this boils down to "I wouldn't use the proposed function
myself", then so be it. I'm happy to leave the people who *would* use
it to thrash out the details and the benefits.

Paul.



More information about the Python-ideas mailing list