Passing to a function -- object and method names (or references)

Discussion in 'Python' started by Midas, Jan 2, 2004.

  1. Midas

    Midas Guest

    Greetings everyone,

    I'm including code, for cut/paste, to better explain my question. I create a Car object then I call some of its methods. No problem. Then I try to pass, to a function, the name of the Car object and the name of one of its methods. I found one way to get this to work but can someone show me a more orthodox way? Is there a way using references to the object and somehow the method?

    Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
    IDLE 0.8

    class Car:
    def __init__(self):
    self.milespergallon=25.0
    self.gas=20
    self.travelled=0
    def drive(self, miles):
    self.travelled=self.travelled+miles
    self.gas=self.gas-(miles/self.milespergallon)


    # a test

    >>> carObjA=Car() ; carObjA.drive(100) ; print carObjA.gas

    16.0

    # Next, trying to pass object and method "references", to someFuncA

    def someFuncA(objArg1,strArg2):
    print carObjB.strMethB

    >>> carObjB=carObjA ; strMethB="gas" ; someFuncA(carObjB,strMethB)

    Traceback (most recent call last):
    File "<pyshell#16>", line 1, in ?
    someFuncA(carObjB,strMethB)
    File "<pyshell#15>", line 2, in someFuncA
    print carObjB.strMethB
    AttributeError: Car instance has no attribute 'strMethB'

    # Next, trying to pass object and method "references", to someFuncB

    def someFuncB(strArg1,strArg2):
    e = "print " + strArg1 + "." + strArg2
    exec e

    >>> strObjB="carObjA" ; strMethB="gas" ; someFuncB(strObjB,strMethB)

    16.0

    # That worked but is there a more orthodox way to pass these "references"?
     
    Midas, Jan 2, 2004
    #1
    1. Advertising

  2. Midas

    Peter Otten Guest

    Midas wrote:

    > I'm including code, for cut/paste, to better explain my question. I create
    > a Car object then I call some of its methods. No problem. Then I try to
    > pass, to a function, the name of the Car object and the name of one of its
    > methods. I found one way to get this to work but can someone show me a
    > more orthodox way? Is there a way using references to the object and
    > somehow the method?


    You can access attributes with the getattr() builtin.

    carGas = getattr(car, "gas")

    You can refer to a method either directly

    fun = car.drive
    fun(123)

    or via getattr():

    fun = getattr(car, "drive")
    fun(321)

    I've modified your example to demonstrate this.

    class NoGas(Exception): pass

    class Car:
    def __init__(self):
    self.milespergallon = 25.0
    self.gas = 20
    self.travelled = 0
    def drive(self, miles):
    newGas = self.gas - miles/self.milespergallon
    if newGas < 0:
    self.travelled += self.milespergallon*self.gas
    self.gas = 0
    raise NoGas("%s miles travelled. No more gas" %
    self.travelled)
    self.travelled += miles
    self.gas = newGas


    def printAttr(obj, attrname):
    "Demo for accessing an attribute by its name"
    print attrname, "=", getattr(obj, attrname)

    def callMethod(obj, methodname, *args):
    """ Demo for calling a method determined by its name.
    An arbitrary number of arguments is just passed
    through to the method.
    """
    method = getattr(obj, methodname)
    print "calling", methodname
    method(*args)

    def callMethod2(method, *args):
    """ Demo for calling a method reference.
    The method is is generated by obj.method in the
    calling code. Of course you can pass function
    references as well
    """
    method(*args)

    def someFunction():
    print "Welcome to the Python Motorshow"

    callMethod2(someFunction)
    car = Car()
    printAttr(car, "gas")
    callMethod(car, "drive", 10)
    printAttr(car, "gas")
    while True:
    printAttr(car, "travelled")
    callMethod2(car.drive, 100)

    Note that my car will not travel as far as yours as it is less satisfied
    with negative amounts of gas :)

    Peter
     
    Peter Otten, Jan 2, 2004
    #2
    1. Advertising

  3. Midas

    Midas Guest

    Peter Otten wrote:

    >You can access attributes with the getattr() builtin.
    >
    >carGas = getattr(car, "gas")
    >
    >You can refer to a method either directly
    >
    >fun = car.drive
    >fun(123)
    >
    >or via getattr():
    >
    >fun = getattr(car, "drive")
    >fun(321)
    >
    >I've modified your example to demonstrate this.


    Thank you very much, Peter! It works nicely!

    Midas
     
    Midas, Jan 2, 2004
    #3
    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. James Vanns
    Replies:
    7
    Views:
    7,060
    Evan Carew
    Jan 21, 2004
  2. Ares Lagae
    Replies:
    8
    Views:
    451
    Ares Lagae
    Sep 24, 2004
  3. wanwan
    Replies:
    3
    Views:
    434
    Alex Martelli
    Oct 14, 2005
  4. News123
    Replies:
    2
    Views:
    466
    John Machin
    Nov 26, 2008
  5. Jack Bates
    Replies:
    3
    Views:
    310
    Steven D'Aprano
    Aug 6, 2011
Loading...

Share This Page