Reputation: 3156
Have a python class that is imported in Robot Framework as follows:
Python: MyClass.py
class MyClass:
def __new__(cls, a, b):
# implementation here
Robot Framework: MyTest.robot
*** Settings ***
Library MyClass.py a=1 b=2
Getting the error Error in file 'MyTest.robot': Test Library 'MyClass' expected 0 arguments, got 2.
I understand the error is because MyClass
doesn't have __init__
defined (if it's defined, there's no import error).
My question is: How can I import MyClass.py
, which has __new__
defined but not __init__
?
(without going into the details of why I have __new__
and not __init__
)
Upvotes: 0
Views: 365
Reputation: 385910
According to the python documentation, __new__
will pass the arguments it receives on to the __init__
. Since you didn't create an __init__
that accepts arguments, that's why you get the error.
If
__new__()
returns an instance of cls, then the new instance’s__init__()
method will be invoked like__init__(self[, ...])
, whereself
is the new instance and the remaining arguments are the same as were passed to__new__()
.
If you don't want the __init__
to be called, you might get some help from this question: Possible to prevent init from being called?
Upvotes: 1
Reputation: 1261
__new__
is responsible on instance creation, and __init__
is just initializer after instance is created. First always is called __new__
and then __init__
. __new__
is always @staticmethod
as a default, but you can use @staticmethod
decorator also (same).
class Foo:
@staticmethod
def __new__(cls):
print('Foo.__new__ is called')
obj = super().__new__(cls)
print(obj) # <__main__.Foo object at 0x7f82c1403588> This is "f" object.
return obj
def __init__(self, *args, **kwargs):
print('Foo.__init__ is called !!!')
f = Foo()
Hope, it helps you.
Upvotes: 1