Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add object and name in list
Hi! How can I add in both list (in first fname and in second the person object) every time when I creat a Person?
Secondary how can I add in a list online students?

class Person:
    name_persons = []
    object_persons = []

    def __init__(self, fname, lname, cnp, age):
        self.fname = fname
        self.lname = lname
        self.cnp = cnp
        self.age = age

    def add_person(self, fname):

    def gender(self):
        if str(self.cnp)[0] == 1:
            return "m"
        return 'f'

class Student(Person):
    def __init__(self, nume, prenume, cnp, varsta, medie):
        super().__init__(nume, prenume, cnp, varsta)
        self.medie = medi
In the __init__()
This saves the Person object which has the name as an attribute. No need for two lists. Actually far better than two lists.

Drop the add_person method. gender will always return 'f'.
3lnyn0 likes this post
It works. I would never have thought of that! Thanks!
I'm back with another problem, now I add a person to the list, but if I run the program again, it deletes that person and adds the newly added person. How do I keep the previously added person, should that list be outside the class?
Check Data Persistence
Saving an Object (Data persistence)

Of course, you can also store all necessary information e.g. as regular txt/csv or JSON, etc. format and recreate the objects, e.g.
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 5,081 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,285 Jan-19-2022, 08:33 AM
Last Post: menator01
  AttributeError class object has no attribute list object scttfnch 5 3,574 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 5,694 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,250 Nov-13-2020, 04:59 AM
Last Post: perfringo
  TypeError: 'list' object is not callable Python_Mey 1 2,552 Aug-25-2020, 03:56 PM
Last Post: Yoriz
  converting string object inside a list into an intiger bwdu 4 2,725 Mar-31-2020, 10:36 AM
Last Post: buran
  set a new object node in a linked list via reference oloap 2 2,167 Mar-13-2020, 09:45 PM
Last Post: oloap
  create a list of object with a list of character studenthch 0 1,389 Feb-12-2020, 08:43 AM
Last Post: studenthch
  print all method and property of list object engmoh 4 2,943 Oct-26-2019, 05:33 PM
Last Post: engmoh

Forum Jump:

User Panel Messages

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