Python Forum
apendng to a list within a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
apendng to a list within a class
#1
i am trying to understand classes how to create list within them and how to append to that list.
i guess i missed a basic understanding of appending somewhere. or something else.
but for instance i try to append to a list like this.
when i run this and enter "grape" to be the appended item , nothing gets returned..

class Fruit:
    fruit_list = []
    def __init__(self,name):
        self.name = name

    def named(self):
        print("{} is a fruit".format(self.name))

    def append1(self):
        inp = input("add a fruit: ") 
        if input == " ":
            self.fruit_list.append(inp)
            return
        self.fruit_list
        

apple = Fruit('apple')
apple.append1() 
Reply
#2
Your input() method asks for a fruit (line 10).

On line 11, you're comparing the input function against a single space. This will never be true, so execution goes to line 14 and the method exits without doing anything.

I assume you meant to compare it to inp instead of input. If you did that and the fruit you entered was a single space, it would add that space it to the list (lines 11-12).
If it were any other string, nothing is done and fruit_list remains the same.

Even if you had modified the list, nothing in this script will show the contents. You'd need to print the attribute (or do something with it) to see the results.
Reply
#3
So like this?
class Fruit:
	fruit_l=[]
	def __init__(self,name):
		self.name=name
		
	def named(self):
		print('{} is a fruit'.format(self.name))
		
	def list1(self):
		inp = input('enter a fruit: ')
		if inp:
			self.fruit_l.append(inp)
			print(self.fruit_l)

apple = Fruit('apple')
apple.list1()
Reply
#4
That looks more functional. I'm not sure why a "Fruit" object should have another list of fruits, or how it relates to itself, but that code should append to the list and display it.
Reply
#5
Couple of observations:
1. It does not make sense for a Fruit class to have a list to add more fruits. Fruit class should be one kind of fruit. You can have a list, outside the class to collect different Fruit instances.
2. fruit_l is class attribute (not an instance attribute) and mutable, it will be shared among all instances of Fruit class, so prepare for a surprise/gotcha experience.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add class instance attributes from list 999masks 2 1,003 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Generate list in class P13N 7 1,927 Dec-28-2018, 10:08 PM
Last Post: P13N
  How to check if class instance exists in a list of class instance objects? sonicblind 23 9,577 May-27-2018, 05:44 AM
Last Post: buran
  Creating a list of class objects hjuyrfc 2 1,609 Jul-22-2017, 07:41 PM
Last Post: hjuyrfc
  List-Elements as instances of a class BigMan 3 2,321 Mar-25-2017, 06:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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