In python the following code works perfectly fine.

class Table:

table = Table()

table.fruits = ["apple", "orange"]

But as soon as we check it out with some kind of linter, errors will start showing. Pylint will complain about attribute defined outside __init__ method, Mypy will claim we're trying to access attribute that doesn't exist etc.

Meanwhile, there are situations when the logic of our program suggests that this is the most appropriate way. Let's say we have a Playground class object, where we store different information's, depending on to which subclass of the Game class, implementing logic of the game being played on it, we pass it to.

Creating a subclass is not a solution if there are reasons to recycle the same object over and over again. Using a dict underneath makes it possible to silence errors, but it seems to be a trick rather than the right solution, since this type was designed to map two collections of objects.

What is the correct approach?

  • Using a dictionary is not "a trick", it is the usual solution to this kind of problem I would start with when using a strictly typed language. But I am wondering by myself if in Python there is an approach to solve this with instance attributes without simply disabling the specific linter rule (which could be also a solution).
    – Doc Brown
    Jun 19, 2020 at 5:56
  • @DocBrown I feel like it's a trick, because from what I understand, original intention was to use dict as a mapping structure, where all keys are the same type and all values are the same type. I know this was often other way around, for example with JSON, and in recent version of python we have TypedDicts for the purpose of using dicts more like classes, but it still is considered just a data structure. And my question is what if we want something more than just a data structure, if we want to have some methods coupled with it.
    – Kasia
    Jun 19, 2020 at 6:24

1 Answer 1


Playground is a dictionary. Linters tell you to make that explicit. It will help to understand the program later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.