[SOLVED]const void * parent (_mp_obj_type_t)

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

[SOLVED]const void * parent (_mp_obj_type_t)

Post by jickster » Mon Mar 26, 2018 9:07 pm

What is this supposed to be used for?
Last edited by jickster on Mon Mar 26, 2018 9:38 pm, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: const void * parent (_mp_obj_type_t)

Post by dhylands » Mon Mar 26, 2018 9:17 pm

I guess that depends on the context. Where are you seeing this?

It looks like a C declaration for a function called parent that returns a const void pointer and takes an _mp_obj_type_t as an argument.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: const void * parent (_mp_obj_type_t)

Post by jickster » Mon Mar 26, 2018 9:20 pm

dhylands wrote:
Mon Mar 26, 2018 9:17 pm
I guess that depends on the context. Where are you seeing this?

It looks like a C declaration for a function called parent that returns a const void pointer and takes an _mp_obj_type_t as an argument.
It's defined in _mp_obj_type_t

Code: Select all

    // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
    const void *protocol;

    // A pointer to the parents of this type:
    //  - 0 parents: pointer is NULL (object is implicitly the single parent)
    //  - 1 parent: a pointer to the type of that parent
    //  - 2 or more parents: pointer to a tuple object containing the parent types
    const void *parent;

    // A dict mapping qstrs to objects local methods/constants/etc.
    struct _mp_obj_dict_t *locals_dict;
};
The easiest starting point for me to understand this is

Code: Select all

#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
const mp_obj_type_t mp_type_ordereddict = {
    { &mp_type_type },
    .name = MP_QSTR_OrderedDict,
    .print = dict_print,
    .make_new = dict_make_new,
    .unary_op = dict_unary_op,
    .binary_op = dict_binary_op,
    .subscr = dict_subscr,
    .getiter = dict_getiter,
    .parent = &mp_type_dict,
    .locals_dict = (mp_obj_dict_t*)&dict_locals_dict,
};
#endif

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: const void * parent (_mp_obj_type_t)

Post by dhylands » Mon Mar 26, 2018 9:35 pm

When you use inheritance, parent will point to the parent object.

So if you were to declare:

Code: Select all

 class Employee(Person):
then the Employee class parent would point to the Person class.

If you're using multiple inheritance:

Code: Select all

class SubClass(BaseClass1, BaseClass2):
then parent will point to a tuple containing BaseClass1 and BaseClass2.

Your example shows that OrderedDict is a subclass of dict.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: const void * parent (_mp_obj_type_t)

Post by jickster » Mon Mar 26, 2018 9:38 pm

dhylands wrote:
Mon Mar 26, 2018 9:35 pm
When you use inheritance, parent will point to the parent object.

So if you were to declare:

Code: Select all

 class Employee(Person):
then the Employee class parent would point to the Person class.

If you're using multiple inheritance:

Code: Select all

class SubClass(BaseClass1, BaseClass2):
then parent will point to a tuple containing BaseClass1 and BaseClass2.

Your example shows that OrderedDict is a subclass of dict.
Thanks. That's what I thought it was but good to make sure.

agonnen
Posts: 27
Joined: Sat Oct 13, 2018 7:52 pm

Re: [SOLVED]const void * parent (_mp_obj_type_t)

Post by agonnen » Wed Nov 28, 2018 8:44 am

@dhylands can we use `parent` to extend a base class with a sub class (with additional members) when extending Micropython?
The other option would be to add all the base's members in each of the child's local-dicts, which is a duplication and bloats the childs.
(related: https://github.com/micropython/micropython/issues/1159)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: [SOLVED]const void * parent (_mp_obj_type_t)

Post by dhylands » Wed Nov 28, 2018 5:31 pm

I'm not sure - I typically look at other code for examples. I know that extending C declared classes in python has some issues. Doing everything in python seems to be ok. I haven't personally played with extending C declared classes using C.

Post Reply