0

I'm creating a little stock management app and I want to access a variable from ClassA in ClassB

ClassA(Screen):
     def test1(self):
          self.username = "Tonny"

ClassB(Screen):
     def test2(self):
          print(ClassA.test1.username)

This code dont work, i already tried like this:

ClassA(Screen):
     def test1(self):
          self.username = "Tonny"
          return self.username

ClassB(ClassA,Screen):
     ClassA = ClassA()
     def test2(self):
          print(ClassA.test1())

The second piece of code should work, but I can't use it because of the screen manager, it stop working right when I put another argument in the class.

Does anyone know how can I do this?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
ATEK0
  • 82
  • 9
  • `ClassA.test1()` attempts to run the method without creating an instance of `ClassA` first, which only works if you make it a `@classmethod` or a `@staticmethod`. You want `ClassA().test1()` to create a class instance and run its method. All of this seems rather superfluous if you are only learning the basics. – tripleee Jan 05 '22 at 12:42

2 Answers2

1

You can do it by sending the instance of ClassA to ClassB in order for ClassB to access it:

class ClassA(object):
    def __init__(self):
        self.username = "Tonny"
        
class ClassB(ClassA):
    def __init__(self, class_a):
        self.username = class_a.username

    def getPassedName(self):
        return self.username

object1 = ClassA()
object2 = ClassB(object1)
username = object2.getPassedName()
print(username)

For more info check this question.

Oghli
  • 2,200
  • 1
  • 15
  • 37
  • i cant really do this because I'm only working inside the classes, and that code outside classes don't work inside one. – ATEK0 Jan 05 '22 at 13:03
  • @ATEK0 You didn't mention that in the requirements of your program. the solution which I provided is the standard way of passing shared variable between classes in object oriented programming. – Oghli Jan 11 '22 at 14:36
1

self.x means a variable x just works inside the class definition. Try this: Define a class A(object), define x=1 before init()

class A(object):
    x = 1
    __init__(self)
    .....

You can address x like A.x.

  • This works! Maybe my mistake was not defining init()...is init() that important? – ATEK0 Jan 05 '22 at 13:13
  • it's actually not the right way of passing variable from one class to another. – Oghli Jan 05 '22 at 13:18
  • @Oghli it worked, and how about you answer whats the right way to do it instead of flaming? – ATEK0 Jan 06 '22 at 18:46
  • init is used to define the inital class variables or values. So, you can use init to declare class variable for exchange with other classes. Maybe you wont use init, you can share variable with other classes in the described way. –  Jan 08 '22 at 09:25
  • @ATEK0 Not everything work considered right and compatible of the standards of writing efficient and clean code. – Oghli Jan 10 '22 at 12:38
  • 'you can use init to declare class variable for exchange with other classes' but only it will work on the class level so it considered a special case. but if you want to exchange shared variables between instance of classes for sure it will not work. – Oghli Jan 10 '22 at 12:40