Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate list in class
#1
Is it possible to generate a list with the inputs in my function addperson, which I can print then with my function printperson? I'm a bit confused with that assignment at the moment. Thanks a lot!

class person:
    def __init__(self, name, id, age):
        self.name = name
        self.id = id
        self.age = age

    def addperson(self):
        name = input("Please enter name:")
        id = id(name)
        age = input("Please enter age:")
        #list with name, id and age should be generated 

    def printperson(self):
        print(self.id, self.name, self.age)
Reply
#2
You could create a list and assign it to an attribute, just like you did in __init__(). Then, printperson() could print that attribute. However, I doubt that's the assignment. Since the class is called "person", it would not make sense to store data for multiple persons in it. What exactly is the assignment?
Reply
#3
I need to generate a list (with command line input) similar to an address book/ phone book, where you can add, print, search and update information. Goal from that assignment should be to get familiar with the use of classes but somehow I don't get how to use list within classes and access them from outside those classes. Any help is highly appreciated.
Reply
#4
In that case, I recommend you make two classes: one called Phonebook and one called Person.

Phonebook should have:
  • an attribute that is a list of Person objects
  • a method to add new Person objects
  • a method for displaying the list (this lets you access it from outside the class)
  • a method for searching the list for a specific person

Person should have:
  • the name, ID, contact info, and what have you
  • a method for returning the above data in a list or string
  • a method for updating the person's information

Give that a try and we'll go from there.
Reply
#5
Thanks for the input. Shouldn't it work with one class (Phonebook) only?

class Phonebook:
    def __init__(self, name, id, age, contact):
        self.name = name
        self.id = id
        self.age = age
        self.contact = contact

    def add_pb_entry(self):
        name = input("Please enter name:")
        id = input("ID: ")
        age = input("Please enter age:")
        contact = input("Please enter contact information")
        newentry = []
        new = Phonebook.add_pb_entry(name, id, age, contact)
        newentry.append(new)

    def update_contact(self):
        searchid = input("Please enter the ID of the entry you want to update:")
        if searchid == id:
            contact = input("Please enter contact information")

    def print_pb_entries(self):
        print(self.name, self.id, self.age, self.contact)
Reply
#6
It can work with only Phonebook, but only if you store the contacts' information in a dictionary or in a list of lists. As you've written it, Phonebook can only store data for a single person. The attributes listed in __init__() are basically variables. Since none of them can store multiple data points, you can only have one person's information loaded at a time.

The add_pb_entry() method has several problems. The add_pb_entry() call on line 14 shouldn't work. add_pb_entry() only takes self as an argument so the function call on line 14 should raise an error.

Even if that call did work, appending to newentry won't accomplish anything because newentry is a local variable for the method. You can correct that by calling it self.newentry which would make a new attribute. Again though, that list needs something to contain and Phonebook currently runs the risk of creating an object that contains an object which contains another object, etc. instead of containing them all in a single data structure (e.g. a list).

With what you have, the easiest way to get to your desired end is to make self.entries (to replace newentry) to contain all the contact data in a list of lists. That attribute should be written in one of two ways:

class Phonebook:    
    def __init__(self, name, id, age, contact):
        self.entries = [[name, id, age, contact]]
 
    def add_pb_entry(self):
        name = input("Please enter name:")
        id = input("ID: ")
        age = input("Please enter age:")
        contact = input("Please enter contact information")
        self.entries.append([name, id, age, contact])
 
    def update_contact(self):
        searchid = input("Please enter the ID of the entry you want to update:")
        if searchid == id:
            contact = input("Please enter contact information")
 
    def print_pb_entries(self):
        print(self.entries)
Reply
#7
in addperson(), name and age should be class variables, i.e. self.name and self.age, which are visible throughout the class and remain as long as the class instance remains. name and age are local and so are garbage collected when the function exits.
Reply
#8
Thanks all for the replies!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  apendng to a list within a class gr3yali3n 4 377 Feb-16-2021, 06:30 AM
Last Post: buran
  How to generate a log in a list style? wagnergt12 5 822 Apr-22-2020, 12:47 PM
Last Post: buran
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 757 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 857 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  how to add class instance attributes from list 999masks 2 1,029 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Generate a list of numbers within a list of arbitrary numbers Takeshio 4 1,409 Nov-08-2018, 12:29 AM
Last Post: Takeshio
  How to check if class instance exists in a list of class instance objects? sonicblind 23 9,613 May-27-2018, 05:44 AM
Last Post: buran
  How to generate the list I want? Krookroo 6 1,976 Sep-07-2017, 02:06 PM
Last Post: Krookroo
  Creating a list of class objects hjuyrfc 2 1,614 Jul-22-2017, 07:41 PM
Last Post: hjuyrfc
  List-Elements as instances of a class BigMan 3 2,333 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