Skip to content
Advertisement

Kivy Python: confused with class and instance methods

I was trying kivy and while I was accessing class variables, I was forced to use ‘self’ for class variables or else it was producing error.

.kv file code

This is the kivy code I was using. Below is the main.py code where I have confusion:

Code where confusion lies

This code works. Here I am using class variables and accessing them from ‘self’.

According to me the code that should work is: Code that should work

OR

Code that should work

But both of these codes give error. What is it that I am doing wrong?

Advertisement

Answer

Kivy properties are Python descriptors, an object supporting a special api that let’s them provide instance-level behaviour when accessed from a class instance.

I think you also have a misconception about class vs instance level variables: variables defined at class level are also accessible via instances of the class, it’s normal that you can access self.something_defined_at_class_level.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement