using attributes as defaults

Westley Martínez anikom15 at gmail.com
Sat Feb 5 00:32:08 CET 2011


On Fri, 2011-02-04 at 13:08 -0800, Wanderer wrote:

> I want to give the option of changing attributes in a method or using
> the current values of the attributes as the default.
> 
> class MyClass():
>      """  my Class
>      """
>      def __init__(self):
>           """ initialize
>           """
>           self.a = 3
>           self.b = 4
> 
>      def MyMethod(self, a = self.a, b = self.b)
>           """ My Method
>           """
>           self.a = a
>           self.b = b
>           DoSomething(a, b)
> 
> The above doesn't work. Is there a way to make it work?
> 
> Thanks
> 

This doesn't work because you can't reference keyword arguments in the
keyword argument array. This will work:
class MyClass:

    def __init__(self):
        """ initialize

        Really? These are the worst docstrings ever.

        """
        self.a = 3
        self.b = 4

    def MyMethod(self, a=None, b=None)
        if a is not None:
            self.a = a
        if b is not None:
            self.b = b
        DoSomething(a, b)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110204/da22fcbd/attachment.html>


More information about the Python-list mailing list