Passing data attributes as method parameters

Discussion in 'Python' started by Panos Laganakos, Apr 24, 2006.

  1. 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.
     
    Panos Laganakos, Apr 24, 2006
    #1
    1. Advertising

  2. Panos Laganakos wrote:
    > 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.


    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
     
    Ben Cartwright, Apr 24, 2006
    #2
    1. Advertising

  3. 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.
     
    Panos Laganakos, Apr 24, 2006
    #3
  4. Panos Laganakos

    Jay Parlar Guest

    On Apr 23, 2006, at 4:59 PM, Panos Laganakos wrote:

    > 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.
    >


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

    As an example:

    >>> class MyClass:

    .... def my_method(self, param1 = []):
    .... print param1
    .... param1.append(5)
    ....
    >>> x = MyClass()
    >>> x.my_method()

    []
    >>> x.my_method()

    [5]
    >>> y = MyClass()
    >>> y.my_method()

    [5, 5]
    >>> y.my_method()

    [5, 5, 5]
    >>>



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

    And an even more illustrative example:

    >>> class M:

    .... 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.
     
    Jay Parlar, Apr 24, 2006
    #4
  5. >>>>> "Panos Laganakos" <> (PL) wrote:

    >PL> Thanks Ben.
    >PL> What does it mean that they're statically bound?


    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.

    >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.


    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 <>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Apr 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict

    passing parameters to run() method

    cppaddict, Jul 8, 2004, in forum: Java
    Replies:
    3
    Views:
    9,811
    xarax
    Jul 16, 2004
  2. Jason
    Replies:
    2
    Views:
    512
    Jonathan Mcdougall
    May 13, 2006
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    377
    Bruno Desthuilliers
    Jun 20, 2007
  4. Ramon F Herrera
    Replies:
    8
    Views:
    483
    Pascal J. Bourguignon
    Sep 13, 2009
  5. Jayden
    Replies:
    16
    Views:
    528
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page