Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add object and name in list
#1
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?
Thanks!

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

    @staticmethod
    def add_person(self, fname):
        self.name_persons.append(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
Reply
#2
In the __init__()
self.persons.append(self) 
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
Reply
#3
It works. I would never have thought of that! Thanks!
Reply
#4
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?
Reply
#5
Check Data Persistence
and
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. https://stackoverflow.com/a/33245595/4046632
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

Reply


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

Forum Jump:

User Panel Messages

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