0

Given instances from two different Classes in Python, I would like to combine them to produce either a new object which has "memory" of the combined objects or a modified instance that conserves its initial properties but develops new ones (as done here).

Obviously, the example below is stupid, but is made for explanatory purposes.

So I have the object foo (class MainObject) which should be combined with different instances of the class OneNumber (here, a, b and c). I need to:

  1. keep the original objects (a, b, and c) or initial fields (of foo) unchanged;
  2. keep track of these and use them (i.e., my final object should be able to use "inside itself" the initial OneNumber and MainObject instances to use their fields and produce new things, here through the self.getNewNumber method);
  3. be able to call the relevant resulting combinations individually (i.e., here "1st" for the [avsfoo] combination, "2nd" for [bvsfoo], etc.).

To address the 3 above, I create a dictionary to store individual combinations. Then, how to ideally create the keys? Obviously, it is suboptimal here. Giving a self.name field to the OneNumber class could be a better (?) option.

But I am sure real programmers must have a better way to do this all, right?

Would for example creating a new "combination" Class (with one instance of OneNumber and the instance of MainObject passed as arguments - three times to create three new objects, then) be better?

class OneNumber():
    def __init__(self, n):
        self.number = n

class MainObject():
    def __init__(self, n):
        self.number = n
        self.dic = {}
        self.newnumber = {}
        self.keys = ['3rd', '2dn', '1st']

    def combineOneNumber(self, onenum):
        combname = self.keys.pop()
        self.dic[combname] = onenum
        self.getNewNumber(combname)

    def getNewNumber(self, combname):
        self.newnumber[combname] = self.number + self.dic[combname].number


a = OneNumber(8)
b = OneNumber(13)
c = OneNumber(23)

foo = MainObject(2)
foo.combineOneNumber(a)
foo.combineOneNumber(b)
foo.combineOneNumber(c)
print(foo.newnumber)
# {'1st': 10, '2dn': 15, '3rd': 25}
ztl
  • 2,512
  • 1
  • 26
  • 40

1 Answers1

0

I do not actually know the purpose of this code. But I change some parts of code, that may be helpful. If you want to write cleaner code, I suggest you read The "clean code" and "python tricks".

class OneNumber():
def __init__(self, number):
    self.number = number

def __repr__(self):
    return f"OneNumber({self.number})"


class MainObject():
    def __init__(self, number):
        self.number = number
        self.dic = {}
        self.new_number = {}
        self.keys = ['3rd', '2dn', '1st']

    def __repr__(self):
        return f'MainObject{self.new_number}'

    def comibne_numbers(self, *args):
        for number in args:
            combname = self.keys.pop()
            self.dic[combname] = number
            self.set_new_number(combname)

    def set_new_number(self, combname):
        self.new_number[combname] = self.number + self.dic[combname].number


a = OneNumber(8)
b = OneNumber(13)
c = OneNumber(23)

foo = MainObject(2)
foo.comibne_numbers(a, b, c)
print(foo)
  • Thanks. But appart from adding the `__repr__` method, what are you doing? And why are you doing this? Explaining the usefulness of adding `__repr__` and how/where you'd use it here would enhance your answer ;-) Sorry I was not clear in my question... Basically, I am looking for a way to "sum" or "combine" two Class instances into a new object with new properties but with memory of the "parents"... – ztl Jan 12 '21 at 09:51
  • Hi, please see this link to know the purpose of dunder repr in python. I hope it is helpful. Good luck. [link](https://stackoverflow.com/questions/1984162/purpose-of-pythons-repr) – Parsa rahbari Jan 12 '21 at 11:35