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>")
Related
I am trying to implement an interface and this interface is taken by two concrete classes say class First and class Second, I have another class that takes these two classes as a parent class CO. The class CO makes a decision based on a flag to return which of the two inherited classes, so as to use their implementation.
from abc import ABC, abstractmethod
class common(ABC):
#abstractmethod
def firstfunction(self):
pass
#abstractmethod
def secondfunction(self):
pass
class First(common):
def __init__(self)-> boto3:
self.ert = "danish"
# self.username = kwargs["username"]
# self.password = kwargs["password"]
print("Inside First function")
def firstfunction(self):
print("My implementation of the first function in FIRST CLASS")
def secondfunction(self):
print("My implementation of the second function in FIRST CLASS")
class Second(common):
def __init__(self):
self.rty = "pop"
# self.session_id = kwargs["session_id"]
print("Inside Second function")
def firstfunction(self):
print("My implementation of the first function in SECOND CLASS")
def secondfunction(self):
print("My implementation of the second function in SECOND CLASS")
class CO(First, Second):
def __init__(self):
self.inst = self.jo()
def jo(self):
a = True
if a:
main_object = First()
return main_object
else:
main_object = Second()
I am instantiating and calling the methods
mymainclass = co()
objt = mymainclass
objt.firstfunction()
objt.secondfunction()
So my condition is if the flag a = True in the CO class then the Concrete class First implementation of the methods should be used and we should get the output like this:
Inside the First function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
If the flag a = False in the CO class then concrete class Second should be used and we should get the output like this:
Inside the Second function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
From the given code I am getting the following output for the flag a = False :
Inside the Second function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
Can someone make this code work? What am I doing wrong? I know I can make a function that takes these two classes as variables and then return what I want based on a condition. But I want to use classes for the same
I would not inherit class CO from either First or Second because you want to end up with an object that is either an instance of class First or class Second. The most straightforward way of doing this is to define method __new__ which will give you more control over instance creation.
Keep all of your code the same (after fixing the obvious errors, such as boto3 not being defined) and changing class CO as follows:
class CO:
def __new__(cls):
a = True
if a:
main_object = First()
else:
main_object = Second()
return main_object
o = CO()
print(type(o))
Prints:
Inside First function
<class '__main__.First'>
If you want to better parameterize the instance creation (and simplify the code within __new__), then have variable a be an argument to CO:
class CO:
def __new__(cls, a):
return First() if a else Second()
o = CO(False)
print(type(o))
Prints:
Inside Second function
<class '__main__.Second'>
You could optionally have class CO have common as its base class to document that instantiating this class results in an instance that implements the common interface. But doing so will not result in enforcing the actual type that is returned by __new__:
class CO(common):
def __new__(cls, a) -> common:
# The Python interpreter is not type-checking what is returned:
#return First() if a else Second()
return []
o = CO(False)
print(type(o))
Prints:
<class 'list'>
You can also substitute a factory function for class CO:
def common_factory(a):
return First() if a else Second()
o = common_factory(False)
Note
If you want to ensure that classes that inherit from base class common must override both firstfunction and secondfunction to more closely emulate a pure interface as implemented in other object-oriented languages, then you should define these functions in common so that they raise an NotImplementedError exception:
class common(ABC):
#abstractmethod
def firstfunction(self):
raise NotImplementedError
#abstractmethod
def secondfunction(self):
raise NotImplementedError
Unfortunately, the enforcement is done at run time with its attendant surprises instead of at compile time as is done in other object-oriented languages.
Okay I think in this situation, CO class must come from common class and implement the first and second function. In the implementation, it will use the First or Second classes functions depends on the result of jo function. Here is the correct Co class code:
class CO(common):
def __init__(self):
self.inst = self.jo()
def jo(self):
a = False
if a:
return First()
return Second()
def firstfunction(self):
self.inst.firstfunction()
def secondfunction(self):
self.inst.secondfunction()
a = CO()
a.firstfunction()
a.secondfunction()
You can directly call the method you want by using the class name.
Unless you have some pressing reason to do this that you didn't include in your question, I would avoid this in favor of a factory function that returns either a First or Second instance depending on the inputs.
class common(ABC):
#abstractmethod
def firstfunction(self):
pass
#abstractmethod
def secondfunction(self):
pass
class First(common):
def __init__(self):
self.ert = "danish"
super().__init__(self)
def firstfunction(self):
print("My implementation of the first function in FIRST CLASS")
def secondfunction(self):
print("My implementation of the second function in FIRST CLASS")
class Second(common):
def __init__(self):
self.rty = "pop"
super().__init__(self)
def firstfunction(self):
print("My implementation of the first function in SECOND CLASS")
def secondfunction(self):
print("My implementation of the second function in SECOND CLASS")
class CO(First, Second):
def __init__(self):
super().__init__(self)
def use_first(self):
return True or False # whatever logic you have for determining this
def firstfunction(self):
if self.use_first():
return First.firstfunction(self)
else:
return First.firstfunction(self)
I am getting an error when making a third class that inherits the first two classes' attributes. The first class's function will be go through but when accessing the second class's function I am gettting an error:
class3' object has no attribute 'othernum
Here is the code:
class class1():
def __init__(self):
self.number = 10000
def getNum(self):
return self.number
class class2():
def __init__(self):
self.othernum = 1111
def displaynum(self):
return self.othernum
class class3(class1, class2):
pass
newperson = class3()
print(newperson.getNum())
print(newperson.displaynum())
Found the answer.
class class3(class1, class2):
def __init__(self):
class1.__init__(self)
class2.__init__(self)
The answer presented by #Ishaan Sathaye is indeed correct. But be aware that there are several mechanisms for initializing base classes in a multiple inheritance hierarchy. See Calling parent class init with multiple inheritance, what's the right way?, in particular the section with heading All base classes are designed for cooperative inheritance.
So, if your 3 classes were designed for cooperative inheritance, we would have:
class class1():
def __init__(self):
super().__init__()
self.number = 10000
def getNum(self):
return self.number
class class2():
def __init__(self):
super().__init__()
self.othernum = 1111
def displaynum(self):
return self.othernum
class class3(class1, class2):
def __init__(self):
super().__init__()
newperson = class3()
print(newperson.getNum())
print(newperson.displaynum())
I have the following code:
class Parent():
def __init__(self):
self.variable1 = self.method1()
self.variable2 = self.method2()
self.variable3 = self.method3()
self.variable4 = self.method4()
#.........(for example I have 100 variables here)
def method1(self):
return 100
def method2(self):
return 200
def method3(self):
return 300
def method4(self):
return 400
class Third():
def __init__(self):
a = 1
def call(self):
value = self.variable3 + 1
return value
class Child(Parent):
def __init__(self):
super().__init__()
self.getanswer = self.method11()
def method11(self):
value_count = Third().call()
return value_count
obj = Child()
It throwed the following Error:
AttributeError: 'Third' object has no attribute 'variable3'
Here I wanted to send all the values of parent to Third Class through Child class. How can I achieve it? I know that I can pass Parent class variables seperately in directly as a parameter in the class Third as following:
value_count = Third(self.variable3).call()
and change the Third Class constructor accordingly. But I don't want to do it as my Parent class has some time taking operations to do.
Also I don't want to make class Third as child to the class Parent.
So How can I recognize the Parent class variables(variable1, variable2, variable3, variable4, ....) in Third Class through Child Class ?
I would like to call functions defined in a class (1) from a nested class (2) (class 2 defined in the class 1). something similar to the code below:
class firstclass(object):
def __init__(self, *args, **kwargs):
#some code and functions...
def afunction(self):
#some code
class nestedclass(tkinter.Frame):
def __init__(self, parent):
#some code
def anotherfunction(self):
#here I would like to call a function from the class firstclass from a button
self.abutton = tkinter.Button(self.parent, text = "blabla", command = firstclass.afunction)
self.abutton.grid(row = 0, column = 1,columnspan=1)
However I keep getting an error missing required positional argument self. how to give it this argument in the command option?
or any advice how to work around this issue?
thanks a lot in advance for all input
I'm trying to dynamically create a class using type() and assign an __init__ constructor which calls super().__init__(...); however, when super() gets called I receive the following error:
TypeError: super(type, obj): obj must be an instance or subtype of type
Here is my code:
class Item():
def __init__(self, name, description, cost, **kwargs):
self.name = name
self.description = description
self.cost = cost
self.kwargs = kwargs
class ItemBase(Item):
def __init__(self, name, description, cost):
super().__init__(name, description, cost)
def __constructor__(self, n, d, c):
super().__init__(name=n, description=d, cost=c)
item = type('Item1', (ItemBase,), {'__init__':__constructor__})
item_instance = item('MyName', 'MyDescription', 'MyCost')
Why is super() inside the __constructor__ method not understanding the object parameter; and how do I fix it?
Solution 1: Using cls = type('ClassName', ...)
Note the solution of sadmicrowave creates an infinite loop if the dynamically-created class gets inherited as self.__class__ will correspond to the child class.
An alternative way which do not have this issue is to assigns __init__ after creating the class, such as the class can be linked explicitly through closure. Example:
# Base class
class A():
def __init__(self):
print('A')
# Dynamically created class
B = type('B', (A,), {})
def __init__(self):
print('B')
super(B, self).__init__()
B.__init__ = __init__
# Child class
class C(B):
def __init__(self):
print('C')
super().__init__()
C() # print C, B, A
Solution 2: Using MyClass.__name__ = 'ClassName'
An alternative way to dynamically create class is to define a class inside the function, then reassign the __name__ and __qualname__ attributes:
class A:
def __init__(self):
print(A.__name__)
def make_class(name, base):
class Child(base):
def __init__(self):
print(Child.__name__)
super().__init__()
Child.__name__ = name
Child.__qualname__ = name
return Child
B = make_class('B', A)
class C(B):
def __init__(self):
print(C.__name__)
super().__init__()
C() # Display C B A
Here is how I solved the issue. I reference the type() method to dynamically instantiate a class with variable references as such:
def __constructor__(self, n, d, c, h):
# initialize super of class type
super(self.__class__, self).__init__(name=n, description=d, cost=c, hp=h)
# create the object class dynamically, utilizing __constructor__ for __init__ method
item = type(item_name, (eval("{}.{}".format(name,row[1].value)),), {'__init__':__constructor__})
# add new object to the global _objects object to be used throughout the world
self._objects[ item_name ] = item(row[0].value, row[2].value, row[3].value, row[4].value)
There may be a better way to accomplish this, but I needed a fix and this is what I came up with... use it if you can.