Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate list in class
#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


Messages In This Thread
Generate list in class - by P13N - Dec-26-2018, 10:09 PM
RE: Generate list in class - by stullis - Dec-26-2018, 10:18 PM
RE: Generate list in class - by P13N - Dec-27-2018, 05:46 AM
RE: Generate list in class - by stullis - Dec-27-2018, 10:06 AM
RE: Generate list in class - by P13N - Dec-27-2018, 11:24 AM
RE: Generate list in class - by stullis - Dec-27-2018, 07:01 PM
RE: Generate list in class - by woooee - Dec-28-2018, 01:26 AM
RE: Generate list in class - by P13N - Dec-28-2018, 10:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 502 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,947 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Generate a list sasitha96 5 2,420 Sep-20-2021, 07:03 PM
Last Post: sasitha96
  apendng to a list within a class gr3yali3n 4 2,381 Feb-16-2021, 06:30 AM
Last Post: buran
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,366 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to generate a log in a list style? wagnergt12 5 2,808 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,397 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,337 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Appending a list in a class from a callback function snizbatch 5 3,774 Sep-01-2019, 06:27 AM
Last Post: snizbatch
  how to add class instance attributes from list 999masks 2 2,757 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