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


Messages In This Thread
apendng to a list within a class - by gr3yali3n - Feb-15-2021, 11:23 PM
RE: apendng to a list within a class - by bowlofred - Feb-16-2021, 12:04 AM
RE: apendng to a list within a class - by gr3yali3n - Feb-16-2021, 02:38 AM
RE: apendng to a list within a class - by bowlofred - Feb-16-2021, 03:34 AM
RE: apendng to a list within a class - by buran - Feb-16-2021, 06:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,280 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Appending a list in a class from a callback function snizbatch 5 3,663 Sep-01-2019, 06:27 AM
Last Post: snizbatch
  how to add class instance attributes from list 999masks 2 2,662 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Build class to make a Smart list - trying icm63 7 3,387 Mar-28-2019, 08:53 PM
Last Post: icm63
  why my method doesn't find my List in the same class? Scorpio 2 2,356 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  Generate list in class P13N 7 4,254 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 20,009 May-27-2018, 05:44 AM
Last Post: buran
  Create class instances from list of strings pythonck 1 3,576 Sep-18-2017, 06:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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