0
class obj:
    def __init__(self, a):
        self.a = a
class obj2 :
    def __init__(self, a):
        self.a = a

pList = [obj(1),obj(2),obj(3),obj(4),obj(5)]
list = []

for i in pList:
    obj2(i.a)
    list.append(obj2)

for i in list :
    print(i.a)

Hi friend. Im python newBie. I have this code but it doesn't work.
please teach me.. thank you

Traceback (most recent call last):
  File "D:/..py", line 18, in <module>
    print(i.a)
AttributeError: type object 'obj2' has no attribute 'a'

Process finished with exit code 1
1
  • 2
    you're mixing up class with instance. and don't use list for your variables. And don't call your files ..py. Commented Nov 3, 2016 at 13:01

1 Answer 1

9

Because you throw away the instance of obj2 that you create in the list, and then append the class itself. It should be:

for i in pList:
    o2 = obj2(i.a)
    list.append(o2)

Note that this would be more obvious if you used standard naming conventions, calling your classes Obj and Obj2.

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

1 Comment

Thank you Daniel !

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.