0

There are three classes (A, B, C). Class A is master and works with some data from classes B and C. Class B works with data from class C, and an instance of class C is passed to B via A.

The code for the example is below ()

a.py

from b import SomeB
from c import SomeC

class SomeA(object):
    def __init__(self):
        self.object_c = SomeC()
        self.object_b = SomeB(self.object_c)

        self.some_work_with_obj_c_data()
        self.some_work_with_obj_b_data()

    def some_work_with_obj_c_data(self):
        self.object_c.some_var_for_a += 1
        print(self.object_c.some_var_for_a)

    def some_work_with_obj_b_data(self):
        self.object_b.some_var_for_a += 1
        print(self.object_b.some_var_for_a)


if __name__ == "__main__":
    object_a = SomeA()

b.py

class SomeB(object):
    def __init__(self, object_c_from_a):
        self.some_var_for_a = 5678
        self.object_c_from_a = object_c_from_a  # an instance of the SomeC class from C.py
        self.some_work_with_obj_c_data()

    def some_work_with_obj_c_data(self):
        self.object_c_from_a.some_var_for_b += 1
        print(self.object_c_from_a.some_var_for_b)

c.py

class SomeC(object):
    def __init__(self):
        self.some_var_for_b = 1234
        self.some_var_for_a = 4321

In the real case it is two GUI windows (A and B), and some data handler (C).

Essentially, class B doesn't explicitly specify what type of data we're working with (especially if you don't specify with a comment). Also there is no autocomplete code in the IDE (PyCharm for example).

Is there a mechanism for specifying that a variable is a reference to an instance of a particular class? Or there's a more global architecture problem and it shouldn't exist in principle?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

In python you do not have static typing, which is what you desire. Instead, variables are dynamically typed.

The key solution to your problem will be to give the variables speaking names. If for example the variable is called amount_of_songs, you can infer that it is a number. Same comes with other types.

The main method used in python ist called duck typing:

Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing, you do not check types at all. Instead, you check for the presence of a given method or attribute.

Edit: Maybe I missed the question: Do you just want to add type hints to the functions? Then this would be typing in python.

Simon Weid
  • 51
  • 3