Passing data attributes as method parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Panos Laganakos

    #1

    Passing data attributes as method parameters

    Hello,

    I'd like to know how its possible to pass a data attribute as a method
    parameter.

    Something in the form of:

    class MyClass:
    def __init__(self):
    self.a = 10
    self.b = '20'

    def my_method(self, param1=self.a, param2=self.b):
    pass

    Seems to produce a NameError of 'self' not being defined.

  • Ben Cartwright

    #2
    Re: Passing data attributes as method parameters

    Panos Laganakos wrote:[color=blue]
    > I'd like to know how its possible to pass a data attribute as a method
    > parameter.
    >
    > Something in the form of:
    >
    > class MyClass:
    > def __init__(self):
    > self.a = 10
    > self.b = '20'
    >
    > def my_method(self, param1=self.a, param2=self.b):
    > pass
    >
    > Seems to produce a NameError of 'self' not being defined.[/color]

    Default arguments are statically bound, so you'll need to do something
    like this:

    class MyClass:
    def __init__(self):
    self.a = 10
    self.b = '20'

    def my_method(self, param1=None, param2=None):
    if param1 is None:
    param1 = self.a
    if param2 is None:
    param2 = self.b

    --Ben

    Comment

    • Panos Laganakos

      #3
      Re: Passing data attributes as method parameters

      Thanks Ben.

      What does it mean that they're statically bound?

      It seems weird that I'm not able to access variables in the class
      namespace even though these attributes come into existance after class
      instantiation.

      Comment

      • Jay Parlar

        #4
        Re: Passing data attributes as method parameters


        On Apr 23, 2006, at 4:59 PM, Panos Laganakos wrote:
        [color=blue]
        > Thanks Ben.
        >
        > What does it mean that they're statically bound?
        >
        > It seems weird that I'm not able to access variables in the class
        > namespace even though these attributes come into existance after class
        > instantiation.
        >[/color]

        The parameters are "put together" and bound to the method when the
        class is defined, *not* after class instantiation.

        As an example:
        [color=blue][color=green][color=darkred]
        >>> class MyClass:[/color][/color][/color]
        .... def my_method(self, param1 = []):
        .... print param1
        .... param1.append(5 )
        ....[color=blue][color=green][color=darkred]
        >>> x = MyClass()
        >>> x.my_method()[/color][/color][/color]
        [][color=blue][color=green][color=darkred]
        >>> x.my_method()[/color][/color][/color]
        [5][color=blue][color=green][color=darkred]
        >>> y = MyClass()
        >>> y.my_method()[/color][/color][/color]
        [5, 5][color=blue][color=green][color=darkred]
        >>> y.my_method()[/color][/color][/color]
        [5, 5, 5][color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]


        Usually, people use immutable datatypes as default parameter values, so
        it doesn't cause a problem.

        And an even more illustrative example:
        [color=blue][color=green][color=darkred]
        >>> class M:[/color][/color][/color]
        .... x = 2
        .... def my_method(self, param = M.x):
        .... pass
        ....
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "<stdin>", line 3, in M
        NameError: name 'M' is not defined


        The class is still being built when the method is created, so even the
        name "M" doesn't exist yet.




        Ben's solution is probably the best way to do what you're looking for.

        Jay P.

        Comment

        • Piet van Oostrum

          #5
          Re: Passing data attributes as method parameters

          >>>>> "Panos Laganakos" <panos.laganako s@gmail.com> (PL) wrote:
          [color=blue]
          >PL> Thanks Ben.
          >PL> What does it mean that they're statically bound?[/color]

          It means that the default values are evaluated at definition time. At that
          time there isn't a variable 'self' defined. It would only work if the
          defaults would be evaluated at the time the method is called, but that's
          not how Python works.
          [color=blue]
          >PL> It seems weird that I'm not able to access variables in the class
          >PL> namespace even though these attributes come into existance after class
          >PL> instantiation.[/color]

          What do you mean 'variables in the class namespace'? Which variable is in
          the class namespace? Please note that you can access variables in the class
          namespace:

          class MyClass:

          a = 10
          b = 20

          def my_method(self, param1=a, param2=b):
          print param1, param2

          --
          Piet van Oostrum <piet@cs.uu.n l>
          URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
          Private email: piet@vanoostrum .org

          Comment

          Working...