2

I can see code below

class MetaStrategy(StrategyBase.__class__): pass

I am not sure why not just write code like below

class MetaStrategy(StrategyBase): pass

Definition schematic

class StrategyBase(DataAccessor):
    pass

class DataAccessor(LineIterator):
    pass

class LineIterator(with_metaclass(MetaLineIterator, LineSeries)):
    pass

def with_metaclass(meta, *bases):
    class metaclass(meta):
        def __new__(cls, name, this_bases, d):
            return meta(name, bases, d)
    return type.__new__(metaclass, str('temporary_class'), (), {})
3
  • 2
    those would do two completely different things. The first inherits from the class of StrategyBase, which itself is presumably a class, and __call__ is either type or another metaclass. MetaStrategy would inherit from StrategyBse Commented Aug 1, 2022 at 8:13
  • StrategyBase.__class__ is StrategyBase's metaclass, which is entirely different from the class StrategyBase itself. Commented Aug 1, 2022 at 8:19
  • First the user-defined classes extends Object class by default. Also Object class implements a set of methods and variables which are common to all the objects being created in the application. This is the main reason why we have Object class as a base class for all the other classes. So in your case, the inheritance will be either direct or indirect. Commented Aug 1, 2022 at 8:19

1 Answer 1

1

If you call self.__class__ from a subclass instance, self.__class__ will use that type of the subclass.

Any class that is expressly specified while using the class will be used naturally.

Take the example below:

class Foo(object):
    def create_new(self):
        return self.__class__()

    def create_new2(self):
        return Foo()

class Bar(Foo):
    pass

b = Bar()
c = b.create_new()
print type(c)  # We got an instance of Bar
d = b.create_new2()
print type(d)  # we got an instance of Foo
Sign up to request clarification or add additional context in comments.

Comments

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.