Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

> I'm not sure that this should be in the stdlib. The three-line function can 
> be enough for your simple case, and it is too simple for including it in the 
> stdlib. But for general stdlib quality solution it lacks many details.
>
> 1. It doesn't work with classes that implement the constructor as __new__, 
> but not __init__. It may need of using metaclasses with overridden __call__. 
> But how then handle classes with custom metaclasses?
>
> Can you illustrate how you would do it for these kinds of classes?

I wouldn't try to implement the generic partialclass() at first place. 
But if I should, the possible code:

     class PartialMeta(type(cls)):
         __call__ = partialmethod(type(cls).__call__, *args, **kwargs)
     class PartialClass(cls, metaclass=PartialMeta):
         pass
     return PartialClass

Not sure it works.

Another solution -- add a wrapped __new__, but __new__ and __init__ 
should be added only when they where defined in the original class or 
its parenthesis. There may be caveats with inheriting from some builtin 
classes.

> Anyway, I think using __new__ on user classes is extremely rare.

This is not good reason of not supporting them. The library function 
should support all corner cases.

> 2. inspect.signature() doesn't give the correct signature as for partial(). 
> This requires changing the inspect module.
>
> Good point.  I can look into this.
>
> 3. pickling and copying of instances are broken in many cases. Pickling a 
> class is always broken. I afraid that this can't be solved without 
> significant reworking of the pickle and the copy modules.
>
> Okay, interesting, but this doesn't seem relevant to partialclass.

If you generate a class, it should behave correctly. Otherwise you could 
just use partial().

> 4. It adds instance attributes __dict__ and __weakref__ even if they were 
> disabled by using __slots__. This increases the size of instances and breaks 
> some properties.
>
> Can we just copy the parent class' __slots__ to the partialclass return value?

This is not so easy. You should add an empty __slots__ if the original 
class doesn't have __dict__ in instances. And testing that may be 
non-trivial task.

> 6. Many alternate constructors and copying methods are broken. For example 
> the copy() method of partialclass(defaultdict, list) in your example. There 
> is no general solution of this, it should be solved individually for every 
> class.
>
> Ultimately, if pickling works, then copying should work too.  The usual way I 
> do it is implementing __getnewargs__, etc.   This should work fine?

I meant the defaultdict.copy() method, not the copy module,

> Should we discuss this on github?

The bug tracker is the place for the design discussion. GitHub is a 
place for discussing implementation details.

It looks to me that this should first be discussed on Python-ideas. You 
should convince core developers that it is worth to add this feature, 
and provide a plan of solving all implementation problems mentioned above.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33419>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to