I've a class
class Container(ContainerApp):
def __init__(self, config,
image=None,
dns='a.b.c',
image_path=None,
ip=None):
...
It is used in the config of another class as follows:
class BasicTest(TestInterface):
config = {
'timeout': 1000,
'api': MyAPI,
'notification': Container,
}
...
I want to pass an ip
to be used by the Container
class. I tried 'notification': Container(device_ip='1.1.1.1')
in my config but it give me this error:
line 1650, in BasicTest
'notification': Container(ip='1.1.1.1'),
TypeError: __init__() takes at least 2 arguments (2 given)
How do I do this?
It might not be the best solution, but it's doable this way:
config = {
'timeout': 1000,
'api': MyAPI,
# 'notification': Container,
}
config['notification'] = Container(config, ip='1.1.1.1')
Assuming that "this" config
(in BasicTest
) is meant to be "that" config
(passed to Container()
) too, and the question is the "recursion".
Reading between the lines that you want config['notification']
to be a type instead of an instance, ie that you will later instantiate it yourself, but you want to provide a default value for the ip
parameter already:
from functools import partial
...
config = {
'notification': partial(Container, ip='1.1.1.1')
}
An alternative would be lambda *args, **kwargs: Container(*args, ip='1.1.1.1', **kwargs)
, which is essentially what partial
papers over very nicely.
In other words, you want config['notification']
to be a callable (eg a function), which, when called, returns an instance of Container
with a predefined value for one of its arguments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.