last day (15 days later) » 

9:16 AM
0
Q: Trying to understand how Python metaclasses work in this bit of code (along with base classes)

user7959439I have a class like this: class GameEvent: __metaclass__=GameEventMeta do_not_register=True id=None priority=0 default_label=None default_args=[] @classmethod def process(event,handler_id,*args): handler=getattr(event,"on_"+handler_id,None) if callabl...

 
Those are a lot of questions in one. Did you read the documentation? For example, the parameters passed to the meta class are covered in the data model. Note that you may want to switch to Python3 – it introduces the __init_subclass__ hook which would make your meta class obsolete.
 
"So whenever that class is called, it takes an argument of the GameEvent class." No, that isn't a call, that is just part of the class definition statement and it's how you provide subclasses in that statement, but yes, the class definition statement does result in the creation of an class object, and yes, the metaclass __init__ runs.
 
@juanpa.arrivillaga I know thats not the call, but when it is called, is that when the Game Even object is created?
 
@user7959439 no, that isn't something being called. And no, GameEvent object is created. Rather, a class object is created (not an instance of that class). You can read more about the details of exactly when the metaclass is called here
 
@juanpa.arrivillaga I mean when "Event_process_locations_advance" is called. Does my statement apply then?
 
9:16 AM
@user7959439 no. Event_process_locations_advance is never called in the code you've shown. Yes, the syntax is the same, but class Event_process_locations_advance(GameEvent) is not a call. It is just part of a class definition statement.
 
I know. That is called in some other code (I think, haven't gotten to it yet). I'm asking what is the sequence of events when it is called. GameEvent object is created. Metaclass Class object created. That class object runs its init files using variable of GameEvent instance object as arguments (cls, name, bases, dct). Init method populates dictionary.
 
@user7959439 what is the sequence of events for what? When what is called. Give an example. It isn't helpful to say "that is called in some other code". Always provide a minimal reproducible example. Also, it is unclear what you mean by "GameEvent object". Normally, you would say that to refer to an instance of the GameEvent class, but I suspect you may mean the GameEvent class object itself.
 
@user7959439 Same as for a regular class, a metaclass is called when an instance of it (i.e. a class) is created. So GameEventMeta is called when the class statement of GameEvent and Event_process_locations_advance is run to create these classes.
 
So, you say "So whenever that class is called, it takes an argument of the GameEvent class." and that is just fundamentally wrong. Event_process_locations_advance does not take any arguments in it's constructor.
 
@juanpa.arrivillaga Event_process_locations_advance does not take any args in its constructor, you're right. So what does it mean that GameEvent is in parentheses?
 
9:16 AM
@user7959439 again that is simply part of a class definition statement, it is not a function call, it is simply the syntax for how you specify the base-classes of the class being defined in a class definition statement. You really shouldn't be messing with metaclasses until your understand the basics of classes and their definitions, you are just going to get confused.
 
The parentheses in a class statement denote the base classes (and possibly keywords sent to the metaclass in Python3). This is a very basic feature of classes in Python. Please familiarise yourself with classes before attempting to use metaclasses.
 
@MisterMiyagi So youre saying a class is called whenever an instance of it (an object) is created. And a metaclass is called whenever an instance of it (a class object ) is created? And whenever a class is called, it runs the init constructor? Or is the constructor only run to create the instance of the class (as an object?) ? Or is when the class is called, it's metaclass is called which uses its constructor to create an instance of the metaclass which is the class object. Then that class object does the same with its object?
 
@user7959439 the metaclass is never called when a class is called The metaclass code is run once, whenever the class is created/defined. When a class is called, some_object = SomeClass(arg1, arg2) then several things can happen. An __init__ is called if it is defined (some other special cases it might not be called, but those are unusual an unimportant for understanding this). All of this is explained in detail in the documentation, and in the data model
 
Ah okay, I understand that part now, it is not a method or a function that takes GameEvent as an argument, it is telling you how to build the class "Event_process_locations_advance" and to build that class you need to give it a GameEvent class? Sorry yeah I'm not very familiar with metaclasses but this is code that I inherited so now I have to try to figure out how it works.
 
@user7959439 __init__ is a well-defined, well-documented part of Python's class system. It works exactly the same for classes and metaclasses (since they are just classes as well). You seem to be confusing very basic nomenclature of how Python classes work – please try to understand that before trying to understand metaclasses. There is no point in trying to explain such an advanced concept if you are not familiar with the basics. Note that comments are not suitable for discussing this, you may want to move this to a chat.
 
9:16 AM
@user7959439 Honestly, I'm baffled, because this has been explained repeatedly. "to build that class you need to give it a GameEvent class? " no, you don't need to give it anything. That is telling it that the class being defined inherits from GameEvent. Are you familiar with inheritance? Eventually, this will get provided to the metaclass constructor, in a tuple as the bases argument. Note, you can dynamically create a class MyClass = type('MyClass', (MyBaseClass,), {}) You can think of a class definition statement as syntactic sugar for that. But just read the docs.
 
I read over everything you guys wrote and tried to think about it. Classes take as arguments the variables defined in their init constructor. Event_process_locations has no init so it doesn't take any variable to create an instance of that class as an object. Arguments passed to the class specify the parent class that it inherits Event_process_locations is an instance of GameEvent class and a class is called whenever an instance of that class is created. So GameEvent is called. This creates the GameEvent Class Object using the metaclass constuctor. This is only done once.
when I write "Classes take as arguments the variables defined in their init constructor. " I mean instances of those classes. So to create an object of Class A, you have to do x = A (b,c,d) where b,c,d are specified in Class A init constructor. So just like an object is created by a class, a class is created by a metaclass and that creation is a "class object" ?
 

last day (15 days later) »