0

Its possibly a very silly question and I just do not know how to look for this solution or what to search.

What I was trying is

class X:
    def __init__(self, myvariable):
        self.myvariable = myvariable

    def printVar(self):
        print(self.myvariable)


class Z:
    aa = 'test'


class Y(Z):
    #xx = X(self.aa)

    def __init__(self):
        self.xx = X(self.aa)

x = Y()
x.xx.printVar()

it works as expected. I was wondering whether its possible to use as

class Y(Z):
     xx = X(self.aa)
1
  • If you try to run it, you will get an error since self is not defined in the class scope. Commented Jan 10, 2022 at 3:00

1 Answer 1

2

self only refer to the own context of a class.

You can refer to aa with Z.aa:

class Y(Z):
    xx = X(Z.aa)
    print(Z.aa)

->

test
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.