0
class Bin():
a=[]
def g(self,e):
    self.a.append(e)
o=Bin()
o.g(3)
o.g(4)
o.g(5)
o.g(6)

Traceback (most recent call last):
  File "<string>", line 28, in <module>
File "<string>", line 9, in bs
NameError: name 'a' is not defined

how to deal with this error? i wanna append the list inside the class with given elements and use the list 'a' in other class methods too.

1
  • Your indentation is wrong. Commented Jun 4, 2021 at 10:21

1 Answer 1

1

Fix your indentation:

class Bin:
    a = []

    def g(self, e):
        self.a.append(e)


o = Bin()
o.g(3)
o.g(4)
o.g(5)
o.g(6)

print(Bin.a)

Output : [3, 4, 5, 6]

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.