Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97099

Re: Modifying signature of ctor in class

References <5cb33b569cf14dfea9424c006c9abc95@exch.activenetwerx.com> <CALwzidm0s48iKMdyzOYwS+nZtBjh_y-Z2yaNi1=Unvsa_Ok1FQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-09-24 16:19 -0600
Subject Re: Modifying signature of ctor in class
Newsgroups comp.lang.python
Message-ID <mailman.151.1443133217.28679.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Sep 24, 2015 at 4:10 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Sep 24, 2015 at 2:28 PM, Joseph L. Casale
> <jcasale@activenetwerx.com> wrote:
>> I have a class factory where I dynamically add a constructor to the class output.
>> The method is a closure and works just fine, however to accommodate the varied
>> input its signature is (*args, **kwargs).
>>
>> While I modify the doc strings, the ctor sig is not optimal. Without building
>> this a string and using eval, is there anything that can be done about this?
>
> In Python 3.3+ you can attach a Signature object to a function by
> setting the function's __signature__ attribute. In Python 3.4+ the
> __signature__ is used in generating the signature for PyDoc and
> help().

Quick and dirty example:

py> from inspect import Signature, Parameter
py> def foo(*args, **kwargs): pass
...
py> foo.__signature__ = Signature([Parameter('x',
Parameter.POSITIONAL_OR_KEYWORD), Parameter('y',
Parameter.KEYWORD_ONLY)])
py> help(foo)
Help on function foo in module __main__:

foo(x, *, y)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Modifying signature of ctor in class Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-24 16:19 -0600

csiph-web