Is it possible to use a instance property as a default value ?

Chris Mellon arkanes at gmail.com
Thu Nov 1 16:20:05 EDT 2007


On Nov 1, 2007 3:18 PM, stef mientki <stef.mientki at gmail.com> wrote:
> hello,
>
> I would like to use instance parameters as a default value, like this:
>
> class PlotCanvas(wx.Window):
>     def __init__(self)
>         self.Buf_rp = 0
>         self.Buf_wp = 0
>
>     def Draw ( self, x1 = self.Buf_rp, x2 = self.Buf_wp ) :
>
> is something like this possible ?
>

No, because self is not in scope at the time that default arguments
are evaluated. The traditional workaround is to use x1=None, and if x1
is None: x1 = self.Buf_rp.

You can write a decorator to make this a little less boilerplateish.



More information about the Python-list mailing list