Python Forum
List not passing between methods inside a class definition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List not passing between methods inside a class definition
#1
I am trying to create a simple class that inputs a list then appends to the list with a function called "add" which is also defined in the same class.

I keep getting this error: 'list' object has no attribute 'a'

class try1:
def __init__(self, a=[]):
self.a = a
print(a)
return

def add(self, b=None):
self.a.append(b)
print(a)
return


if __name__ == "__main__":
c=try1(['a', 'b', 'c'])
d = ['d', 'e', 'f']
try1.add(d)
Reply
#2
I think there's an indentation problem here. Please post your code with python tags so that we can see the indentation. See the BBCode link in my signature for instructions.

That said, using an empty list as a default parameter is a bad idea. You should use None, and if the parameter is None, replace it with an empty list. Also, I would not give b a default value. The method doesn't make sense without a parameter, so let it cause an error if one isn't given. I also think it may be masking the problem you are having.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for the reply!

I've updated the code per your recommendations :)

class try1:
    def __init__(self, a=None):
        self.a = a
        print(a)
        return
    
    def add(self, b):
        self.a.extend(b)
        print(a)
        return
    
    
if __name__ == "__main__":
    c=try1(['a', 'b', 'c'])
    d = ['d', 'e', 'f']
    try1.add(d)
Reply
#4
Your last line should be c.add(d). You want to call it from the instance, so the instance parameter (self) is automatically filled. Also note that the print in the add method should be print(self.a), since a is an attribute of the instance. Finally, what I was talking about with the a parameter to __init__ was more like this:

def __init__(self, a = None):
    if a is None:
        a = []
    self.a = a
    print(a)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Inside the class, you keep printing 'a' but this is the passed argument which is always the same since you don't alter it. Do you mean to print self.a?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Thanks all!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Return a definition samh625 6 2,454 Jul-28-2020, 08:32 AM
Last Post: samh625
  Please, how do I call the method inside this class Emekadavid 1 1,637 Jun-26-2020, 01:26 PM
Last Post: Yoriz
  Child Class, Loop list bjornnotborg 2 2,394 Aug-28-2019, 12:31 PM
Last Post: bjornnotborg
  Methods that return the highest score from the list erfanakbari1 7 7,148 Mar-26-2019, 08:32 PM
Last Post: aankrose
  New to coding. An error occurs with no definition westernwhiskey 4 2,988 Mar-15-2018, 12:37 PM
Last Post: westernwhiskey
  How do you compute tf-idf from a list without using the counter class syntaxkiller 8 5,199 Dec-01-2017, 05:24 PM
Last Post: nilamo
  trouble importing class definition from one .py into another ijosefson 3 3,347 Oct-16-2017, 08:24 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020