How to write a Python class with different methods in different cases? - python-3.x

Suppose we have two classes A and B
class A:
def __init__(self):
pass
def print_abc(self):
print('abc')
class B:
def __init__(self):
pass
def print_def(self):
print('def')
We use class A in the case when we want to work with storageA, and class B when we want to work with storageB. Now, is there a way to write one class, named C, which uses method print_abc if storageA is in question and print_def if storageB is in question? In some sense hypothetical something like this:
class C:
def __init__(self, storage):
self.storage = storage
#storage=storageA
def print_abc(self):
print('abc')
#storage=storageB
def print_def(self):
print('def')
Is something like this possible and how?

Related

Instantiate parent class only once when calling from two child classes

I would like to know if there's a way that we can instantiate a class only once when called from one of the child class and somehow cache it and use it for second child class without instantiating the parent class.
class A:
def __init__(self):
#some huge data stored in self parameters.
class B:
def __init__(self):
A.__init__(self)
class C:
def __init__(self):
A.__init(self)
So both classes B and C uses class A as a parent class and class A has huge data initialised which can be used by both classes B and C. So what I'm trying to achieve here is when I instantiate class B for example, it instantiates class A and loads all the data into memory and to cache the instance so when I immediately instantiate class C it looks for the existing instance of the class without instantiating it again.
How about instantiating A in B and providing a method to transfer the data from B to outside? See below:
class A:
def __init__(self):
self.huge_data = [.....]
def data(self):
return self.huge_data
class B:
def __init__(self):
self.a = A()
def pass_data(self)
return a.data()
class C:
def __init__(self):
A.__init(self)
Once you instantiate B, you will be able to use the method to access the data.

Python Multiple Inheritance - values not accessible

I have a few classes set up that hold values for an application. I'd like to import them into the same class, however, the values in the last class are being ignored. Here is a simple example.
class A:
def __init__(self):
self.testA = 'A'
class B:
def __init__(self):
self.testB = 'B'
class C(A, B):
def __init__(self):
super().__init__()
print(self.testA)
print(self.testB)
test = C()
The above will work only if I do not include the print(self.testB) the variable in class 'B'. I'd like to access the data from Class B as well. How? Thank you.

Cross class calling through 'sibling' inheritance?

I'm wondering if what I have created is valid and 'nice' - or actually a mess
I have a class that has a method which calls a function in another class. However, it is only 'visible' by the fact these two classes are brought together in another child class. If somebody is just looking at this method they may wonder where this is actually located - it is not clear.
Am I doing something wrong here, or something common in Python?
class A():
def __init__(self):
pass
def register_urc(self, text):
print(text)
class B(A):
def __init__(self):
A.__init__(self)
class C():
def __init__(self):
self.register_urc("Hello World")
class Z(B, C):
def __init__(self):
B.__init__(self)
C.__init__(self)
def test(self):
print("finished")
z = Z()
z.test()
What I get is:
Hello World
finished.
I assume this is working because I'm referencing Z's 'self' and passing that down to all other classes.

Better way than pass the same argument among classes in Python

I have a question related to OOP but it should be implemented in Python.
I have a file user_inputs.py with all the user parameters.
In my main file, I have a function that is called first. This function is responsible to read all the user parameters and return a dictionary that will be used in the rest of the program.
My question is: what is the cleanest way to pass the user_parameters dictionary to all classes? I did it in 2 ways:
Method 1)
def read_user_parameters():
# code to open and read all parameters etc.
return user_parameters # returns a dictionary with all the user parameters
Class A():
def __init__(self, user_parameters):
self.user_parameters = user_parameters
Class B():
def __init__(self, user_parameters):
self.user_parameters = user_parameters
user_parameters = read_user_parameters()
object_A = A(user_parameters)
object_B = B(user_parameters)
I don't like this way because I have dozens of classes that need to pass this argument. So I thought to create a parent class with the user parameters:
Method 2)
Class User_parameters():
def __init__(self, user_parameters):
def read_user_parameters():
# code to open and read all parameters etc.
return user_parameters
Class A(User_parameters):
__init__(self, user_parameters):
super().__init__()
# self.user_parameters comes from the parent class now
Class B(User_parameters):
__init__(self, user_parameters):
super().__init__()
# self.user_parameters comes from the parent class now
object_A = A()
object_B = B()
I prefer method 2, however, when super() is initialized from Class A and Class B the function read_user_parameters() that reads the file will be called twice (multiply this by dozens of times). Is there a better solution than method 1 in which I call read_user_parameters() only once but doesn't need to pass the argument for all classes?
Thank you for your time.
Why not just have a single UserParameters class and two objects of the same class (Also class nameds are supposed to be camel-cases, not snake-cased)
#Single class for user parameters
class UserParameters:
def __init__(self, user_parameters):
self.user_parameters = user_parameters
def read_user_parameters(self):
# code to open and read all parameters etc.
return self.user_parameters
#Two objects
object_A = UserParameters("<params>")
object_B = UserParameters("<params>")

Class inheritance python 3.6 : Similar methods

I'm not the strongest pillar when it comes to class inheritance, so here goes my rather silly question. Following the code below, I would logically assume that after the 'super' call, the pointer arrives at self.example() which would in turn refer to the 'example' method in the same class and value 20 will be printed.
class A(object):
def __init__():
self.example()
def example(self):
print(20)
class B(A):
def __init__():
super().__init__()
def example(self):
print(10)
x = B()
Result : 10
This clearly isn't the case and 10 is printed instead. Could someone please shed some light on the mysterious world of class inheritance.
class A(object):
def __init__():
self.example()
def example(self):
print(20)
class B(A):
def __init__():
super().__init__()
x = B()
x.example()
Look for this, at example.
When you inherit B, from A, then method example is inheritated to B, you not must rewrite this to B. Of course still you can write this method for B, then you will override 'A' method, for objects of class B.
You also can use one class to Inheritance with many others:
class Base(object):
def __init__(self):
print("Base created")
class ChildA(Base):
def __init__(self):
Base.__init__(self)
class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__()
ChildA()
ChildB()
ChildB have another call which is equivalent to that used in example above.

Resources