Trying to do inheritance; getting "TypeError: bases must be 'type' objects"

What I’m trying to do:
I am trying to have an inheritance-basted structure to organise the different kind of components I can draw on my page

What I’ve tried and what’s not working:
I have created a new module, and put a class abstractGague(): in it which implemented some shared functionality. I then went to one of the forms I want to inherit from this class, and changed class thermometerGague(thermometerGagueTemplate): to class thermometerGague(thermometerGagueTemplate, abstractGague):
This has resulted in be getting an error of TypeError: bases must be 'type' objects
I have no idea what exactly is a base here or how do I make it a ‘type’ object

This code works just fine:

>>> class AbstractGague:
...     x = 1
...
... class ThermometerGagueTemplate:
...     y = 2
...
... class ThermometerGague(ThermometerGagueTemplate, AbstractGague):
...     z = 3
...
... t = ThermometerGague()
... print(t.x, t.y, t.z)
1 2 3
>>>

(Common convention in Python class names is to keep the first letter uppercase)

2 Likes

I think @stefano.menci answered the main problem, but just to add on the additional, end question.

The base would be the parent class. A class is an instance of a type. Given that, bases must be 'type' objects can be interpreted as The parent must be a 'class' object, but the original error message is more precise.

1 Like

Wait, so if your example works, where am I getting my error from?

I should probably link the app itself:
https://anvil.works/build#clone:EIFZZR2CMY5I55ZH=7G3IYT5CNNUUOFNS6VTNLNT6

Upd: Okay I found my problem. Because I made a module called abstractGague, with a class abstractGague in it, and then imported the module, I needed to declare thermometerGague with abstractGague.abstractGague, not abstractGague, because the latter is a package name

2 Likes

Normal convention in Python is to name packages and classes using different formats. That would possibly have saved you here.

e.g.

from abstract_guage import AbstractGuage

https://peps.python.org/pep-0008/#package-and-module-names

4 Likes

I read that, too. But I’ve seen so many examples of modules being named after the classes they define, I mistook common practice for “good practice.” Thank you for straightening me out!

1 Like