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
  How to read module/class from list of strings? popular_dog 1 422 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,882 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Generate a list sasitha96 5 2,295 Sep-20-2021, 07:03 PM
Last Post: sasitha96
  apendng to a list within a class gr3yali3n 4 2,291 Feb-16-2021, 06:30 AM
Last Post: buran
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,273 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to generate a log in a list style? wagnergt12 5 2,686 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 2,332 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 2,246 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Appending a list in a class from a callback function snizbatch 5 3,661 Sep-01-2019, 06:27 AM
Last Post: snizbatch
  how to add class instance attributes from list 999masks 2 2,661 Jul-22-2019, 07:59 AM
Last Post: 999masks

Forum Jump:

User Panel Messages

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