简体   繁体   中英

passing *args/**kwargs in a derived class

I need to understand the following behavior

Traceback (most recent call last):
  File "FactoryTest.py", line 7, in <module>
    swift = FactoryLogs.get_service('freezer')
  File "/home/iob/Devel/elastic_tests/elasticFactory.py", line 12, in get_service
    return Factory.create_factory(service)
  File "/home/iob/Devel/elastic_tests/factory.py", line 11, in create_factory
    return FreezerLogs()
  File "/home/iob/Devel/elastic_tests/elasticFactory.py", line 68, in __init__
    super(FreezerLogs, self).__init__()
TypeError: must be type, not classobj

This is raised when I try to create an instance of a derived and call a method defined in its base class.

class A:
  def __init__(self):
    self.att1='a'

  def _foo(self):
    self.bar(*args, **kwargs)

class B(A):
    def __init__(self):
    super(B,self).__init__()

    def bar(self, *args, **kwargs):
      #code

Then I create an instance of type B and I call the bar. I feel that many things go wrong here and i need some to enlighten me because reading the doc and other resources online, i felt more confused.

The error you get indicates that you are probably attempting to use super with old-style classes. Unfortunately, this doesn't work and has to be fixed either by switching to new-style classes or by invoking the superclass without the use of super .

If you are in control of the base classes, make sure that they inherit from object . Otherwise, do not use super , but call the base class constructor directly:

class B(A):
    def __init__(self):
        A.__init__(self)
        # ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM