[Python-ideas] Define a method or function attribute outside of a class with the dot operator
stephanh42 at gmail.com
Fri Feb 10 05:29:32 EST 2017
What about using a simple decorator instead?
return lambda func: setattr(cls, func.__name__, func)
# gives 42
2017-02-10 11:15 GMT+01:00 Matthias welp <boekewurm at gmail.com>:
> Hi Markus,
> Thanks for writing this up, as I've had this same very valid problem
> On 10 February 2017 at 10:13, Markus Meskanen <markusmeskanen at gmail.com>
> > I'm suggesting the addition of support to using a dot notation when
> > a function to be a method of a class, or a callback attribute.
> Your solution to me seems like a 'hack': class monkey-patching during
> is already available if you really need it, and your proposal only
> makes it easier,
> which I don't really like.
> > This functionality would be useful in the few rare cases where the class
> > itself needs to be accessed in the function's definition (decorator,
> > etc
> This problem could just as well be solved by allowing access to a
> variable (__class__? __type__?) which is available in the class body at
> construction time, which points to the (eventual) class type object,
> or evaluating
> the type hints in a class only after the class is created, which then
> allows for that
> class to be pointed to in the type annotations.
> E.G. this does not work right now:
> class A:
> def foo(self: A):
> as it fails with NameError: A is not defined, whereas you'd expect it to
> The problem is very annoying when you're trying to work with the dunder
> methods for e.g. numerical objects, as you cannot say '+ is only allowed
> these types', but it is not limited to this scope only.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas