F
F
fantom_ask2020-05-01 08:54:59
Python
fantom_ask, 2020-05-01 08:54:59

How can I put all parameters into an object while iterating over an array?

I want to shove several parameters into an object that are in an array

class obj(object):
    def __init__(
        self, 
        text='white',
        background='white'
    ):
        print('{}-{}'.format(
            text,
            background
        ))
        # Дальше идет куча другого кода...


def fun(**kwargs) :
    for x in range(2) :
        for key, value in kwargs.items():
            #obj(**kwargs)
            if key == 'text' :
                obj(key=value[x+1])
            else:
                obj(key=value[x])

fun(
    text=[
        'red',
        'blue',
        'green'
    ],
    background=[
        'black',
        'yellow',
        'brown'
    ]
)

fun()

Each fun() function should only generate 2 objects

1
blue-black
green-yellow
2
white-black
white-black

I would like to leave the ability to somehow influence the index of the arrays of each of the parameters separately.
And I would like to avoid using try and extra if else

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deleting Account, 2020-05-01
@Andriy_Kosmenyuk

Can you explain your task properly? There are no arrays in the code at all, only linear lists.

put multiple parameters into an object
what???? And at least observe pep8 a little, please.

I
Ilya Korol, 2020-05-01
@welcome32

obj is an object. It's not an instance of an object, so there's no way you can do it
See OOP Principles

O
o5a, 2020-05-01
@o5a

def fun(**kwargs) :
  for x in range(2) :
    #obj(**kwargs)
    text = kwargs['text'][x+1]
    background = kwargs['background'][x]
    obj(text, background)

But in general, as in the previous question, the implementation methods you have chosen look pretty wild.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question