0

I have this class hierarchy

class ParentMeta(type):
    def __new__(v1, name, b, x):
        # some code
        return super(ParentMeta, v1).__new__(v1, name, b, x)

and then

class ServiceMeta(ParentMeta, AnotherServiceMeta):
    pass

The ServiceMeta is normally used as a decorator in add_metaclass i.e.,

@add_metaclass(ServiceMeta)
class MyService(object):
    """ Code

The question i want to ask that how can i pass name attribute here? The name attribute is part of ParentMeta.

EDIT:

Link to add_metaclass documentation: https://six.readthedocs.io/#six.add_metaclass

By default, the ServiceMeta is picking up MyService as name for the class. I want to modify this behavior. For that reason i want to pass name value in decorator.

Em Ae
  • 8,167
  • 27
  • 95
  • 162
  • "The ServiceMeta is normally used as a decorator in add_metaclass i.e.," huh? what is `add_metaclass`?? – juanpa.arrivillaga Apr 13 '22 at 20:57
  • And why would you need to pass the `name` attribute? It really is not clear at all what you are trying to accomplish – juanpa.arrivillaga Apr 13 '22 at 21:00
  • @juanpa.arrivillaga see updated question – Em Ae Apr 13 '22 at 21:03
  • "By default, the ServiceMeta is picking up MyService as name for the class. I want to modify this behavior. For that reason i want to pass name value in decorator." There's no way to do that really if you are using a class definition statement, you'd have to use your metaclass directly to instantiate your class. So this question really has nothing to do with decorators – juanpa.arrivillaga Apr 13 '22 at 21:04
  • Also, do you *really* need to be using `six`? – juanpa.arrivillaga Apr 13 '22 at 21:04
  • ^ its an existing code base and there isn't much choice to get rid of `six`. I think my only other option is to perhaps modify `ServiceMeta` to accept `name` attribute and then pass to `ParentMeta` via `super`? – Em Ae Apr 13 '22 at 21:11
  • That doesn't sound related at all to what you are talking about. Again, if you *ever* want to pass that parameter to the metaclass, you have to use it directly, i.e `MyClass = MyMeta("SomeOtherName", (), {})` now `MyClass` will be a class object with a `__name__` attribute of `SomeOtherName`, which is what I thought you were trying to do – juanpa.arrivillaga Apr 13 '22 at 21:12
  • Note, `ServiveMeta` **already accepts `name`**. It really isn't clear, again, what you are asking, what is *the actual result* you are trying to achieve – juanpa.arrivillaga Apr 13 '22 at 21:13
  • how `ServiceMeta` already accepts `name`? – Em Ae Apr 13 '22 at 21:14
  • Because, *all metaclasses do*. I mean, you could try to write a metaclass that doesn't accept that parameter, but it wouldn't be very useful as a metaclass (unless maybe it is being used directly, `SomeClass = MyMeta()` where it is internally passing some `name` and the other arguments to `type` – juanpa.arrivillaga Apr 13 '22 at 21:16

0 Answers0