Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
To my understanding, C does not have the concept of objects, then how can Python be implemented in C to support something that C can not? How is the concept of “Object” modeled in C? What is internally used?
Edit : Reading through the comments, I am interested in this specific implementation of Python which “uses” C to make the whole thing work. More specifically how is a python “object” represented in C.
As an example in comments, something like a Banking System can use “Classes” to model it, like so how can C model the Python concept of Object.
Higher level abstractions are routinely implemented with lower level building blocks.
For example, C higher level language constructs are themselves implemented with lower level assembler instructions. And the same applies to object orientation, which is often implemented with non-object oriented techniques.
Objects are made of data and behavior. C does not know objects, but it knows data structures and functions:
To implement “methods” that would operate on data structures in C, to emulate basic object orientation, you can define C functions that take a pointer to the data structure as one of their argument (usually the first). Of course, doing this manually is not practical, and you can easily forget to call the method to initialise an object that was just allocated. But if you have a compiler, it’s easy to make sure that this is done systematically. It’s also easy for to transform a call such as
To implement methods that change depending on some condition, in C you can use function pointers. A function pointers can point to any function that have a given “signature” (i.e. return type and argument with types). This allows to implement some dynamic dispatching such as what is needed for polymorphism. It is then easy to enrich the inital data structure with some function pointers. Or to enrich it with a pointer to another datastructure that holds the name and function pointer of each relevant method corresponding to a guiven class
If the language implemented allows functions/methods with variable number of arguments, you could opt for reduced number of arguments in C, such as the pointer to the data structure holding the object’s data, a pointer to a list of arguments.
If the language implemented allows for dynamic typing, all method arguments discussed so far would simply be a data structure that holds information about the type of the data, and the data itself, i.e. in C a pair of pointers to different structures).
If you need to allow inheritance and multiple inheritance, it gets tricky, but you would work either on generating a composed data structures, or organise the data structure with some pointers that point to a parent object, along with the code to properly access the data following the chain.
And for every other language construct, there is probably already a solution. Because ultimately, the thing will run on a microprocessor with very low-level elementary machine instructions.