Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the same function from two different classes

Tags:

python

oop

I have two classes, suppose A and B. Within B, I instantiate A.

I have a function func() that is required by both the classes.

How should I go about it? I had thought of this approach:

class A:
   func()

class B:
   x = A()
   func()

def func():

And then I can access func() from within A or B. Is this approach OK or is there a better way to do this (perhaps using an OO approach)

Note that I am new to OO programming and that is why I am interested in knowing whether I can apply any OO design to this.

Edit: The function can differ in the arguments it takes.

like image 450
Chris Avatar asked Sep 18 '26 16:09

Chris


1 Answers

Define func before you define either class, and it will be available to both.

like image 155
Alex Bliskovsky Avatar answered Sep 21 '26 06:09

Alex Bliskovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!