0

I want to call a method from classB, in a method in classA and pass arguments:

class A:
     B.processAds(ad, cnx, renewableAds, adsToRenew, webdriver)


class B:
    def processAds(self, ad, cnx, renewableAds, adsToRenew, webdriver):

How would I do this?

Daniel Rusu
  • 115
  • 1
  • 11
  • you have to initialize an instance of class B (`foo = B()`) first and use it in class A (`foo.some_method_from_A(args)`). – cuongnv23 Aug 20 '16 at 00:57
  • 1
    How does `class B` know which instance of `class A` is to be used? Is that passed on instance initialization or in the method call itself? An example runnable script without so many parameters would only be a dozen lines long. How about fleshing this out a bit? – tdelaney Aug 20 '16 at 01:25

1 Answers1

0

Make the method in class B a classmethod:

class B:
    @classmethod
    def processAds(cls, ad, cnx, renewableAds, adsToRenew, webdriver):

then you'll be able to use it without instantiating class B, e.g.:

return_value = B.processAds(ad, cnx, renewableAds, adsToRenew, webdriver)

You can read more about classmethod in this answer.

Community
  • 1
  • 1
rafalmp
  • 3,988
  • 3
  • 28
  • 34
  • 1
    I don't think there is enough information in the question to conclude that a class method will work. OP clearly shows that `processAds` is an instance method. – tdelaney Aug 20 '16 at 01:23