2

I am trying to make my class to be executed a given number of times according to passed parameter. My class converts dictionary into some desired data.

Let me show You a quick example of what i mean exactly:

First class A just offers common methods for derivative classes.

class A(object):
    @staticmethod
    def do_magic(obj):
    # ...
    print(''.obj)

Class B, E (skipped in example) initialise objects and provide b_stuff and e_stuff.

class B(A):
    def __init__(self, mydict):
    #
    self.b_stuff = [...]

Now Class C comes into play. It basically transforms given dictionary mydict which has a key-value pair describing number of iterations 'iters': 'val'

class C(B, E):
    def __init__(self, mydict):
        B.__init__(self, mydict) # access to b_stuff
        E.__init__(self, mydict) # access to e_stuff

        self.iters = int(self.mydict['iters'])     # desired nb of initialisations/executions
        self.c_stuff = [b_stuff, e_stuff, other]        

    def do_magic(self):
        A.do_magic(self.c_stuff) 

Now the problem is, that once calling a class from external loop later in code i don't know how to make my class to do_magic mydict['iters'] number of times.

for d in list_of_dicts:
    C(d).do_magic()

One of solutions could be that i'll initialise this class just in external loop above, like that:

for d in list_of_dicts:
    for i in range(int(d['iters']))
        C(d).do_magic()

But it feels far away from OOP style.

To be 100% sure that You folks follow me, ill show here actual expected behaviour:
d - input dictionary
d['iters'] = 2

Execute

C(d).do_magic()

Actual result:

>>> 
'XXX YYY RANDOM_NUMBER1 .... '  

Expected result:

>>> 
'XXX YYY RANDOM_NUMBER1 .... '  
'XXX YYY RANDOM_NUMBER2 .... '  # random number from class B re-initialised!!

As exploring stack i've found out some hints to refer __new__ method but still no clue if this is a right path and how :)

Edit:

As @Peter-wood suggested - adding to a question:

I'd like to point out that both attributes b_stuff and e_stuff once initialised - produce unique strings that are part of c_stuff and are about to be updated with each do_magic() call.

This method basically does ''.join(obj) (see class A)

1 Answer 1

1
class C(B, E):
    def do_magic(self):
        for _ in range(self.iters):
            A.do_magic(self.c_stuff) 
Sign up to request clarification or add additional context in comments.

2 Comments

This is nice but didn't work exactly as I expected because: In b_stuff i have some random_number that is generated within each initialisation and needs to be unique, solution that You provided refers to the same originally initialised value. But thanks anyway.
@TestuserSemathopy Put that in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.