Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

helpfulness, in my /prog/?

Name: Anonymous 2012-01-06 3:51

Is there a way to do this in python?  I want to have a class that gets passed or creates an object and be able to call this object's methods through the containing class, except the object can be of any class, so inheritance doesn't work.

So basically, I want this to work:


class A:
    def __init__(self, obj):
        #DO STUFF TO IMPORT obj'S METHODS

class B:
    def funcB(self):
        #DO STUFF

class C:
    def funcC(self):
        #DO STUFF

x = A(B)
x.funcB()

x = A(C)
x.funcC()

Name: Anonymous 2012-01-06 4:10

If it aint Proto-Kaw, it's shit. Christian rock FTW!

http://www.youtube.com/watch?v=vOYCBlUXb_0

Name: Anonymous 2012-01-06 6:34

or __getattr__

Name: Anonymous 2012-01-06 6:36

>>1
Use dir() to get list of fields and methods, use type() to tell functions from fields, use __setattr__ to create lambda functions that passes all parameters to real function.

Name: Anonymous 2012-01-06 7:27

Actually inheritance would work, or am i misunderstanding you?
You can put the class definition inside a function like so:

def foo(o):
  class A(o):
    pass
  return A()

class B:
    def funcB(self):
        #DO STUFF

# then use like so
x = foo(B)
x.funcB

Don't change these.
Name: Email:
Entire Thread Thread List