Python Question

Asked 2 years ago, Updated 2 years ago, 15 views

Foo class only

Each

print(Foo.bar)
print(Foo().bar)
print(Foo.Bar.bar)
print(Foo.Bar().bar)

in A, B, C, D should be output.

I don't know where to start. Even if I put the print function in the bar and bar functions in Foo, only the reference value comes out and I can't find the answer.

python

2022-09-20 18:06

1 Answers

You can do it as below.

class Foo():
    bar='A'
    def __init__(self):
        self.bar='B'

    class Bar():
        bar='C'
        def __init__(self):
            self.bar='D'


print(Foo.bar)
print(Foo().bar)
print(Foo.Bar.bar)
print(Foo.Bar().bar)


2022-09-20 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.