There are many people who need to be manually entered into the code. The data about these people will never change, they will only need to be requested occasionally. What is the difference between the two codes below? What is the best option or how best to solve such a puzzle?

class Person: def __init__(self, sex, numbers): self.sex = sex self.numbers = numbers andrew = Person("Male", ("8 800 555 35 35", "8 900 800 70 60")) julia = Person("Female", ("8 999 888 77 66", "8 123 456 78 90")) 

Or:

 class Person: pass andrew = Person() andrew.sex = "Male" andrew.numbers = ("8 800 555 35 35", "8 900 800 70 60") julia = Person() julia.sex = "Female" julia.numbers = ("8 999 888 77 66", "8 123 456 78 90") 

3 answers 3

There are many people who need to be manually entered into the code.

Use the format that is most convenient for the person who enters data, for example, a simple text file in csv format:

 M, 8 800 555 35 35, 89008007060 F, 8 999 888 77 66, 8 123 456 78 90 

or create a small UI for convenience (text or GUI or use an existing interface such as Excel) to check the correctness of the data on the fly (that the field contains only two values ​​(genderqueer does not exist), numbers contain digits, etc.) and write to the database such as sqlite directly (when reading later, you can use row_factory to get the objects of the desired type to get or use the type provided by the ORM such as in sqlalchemy ).

Data about these people will never change

In such cases, it is convenient to use collections.namedtuple , if the object type is specified manually:

 Person = namedtuple('Person', 'sex numbers') andrew = Person("Male", ("8 800 555 35 35", "8 900 800 70 60")) julia = Person("Female", ("8 999 888 77 66", "8 123 456 78 90")) 

In this case, you cannot change the available attributes, for example, as julia.age = 10 and julia.numbers = () prohibited.

If you want to change existing attributes, add new ones, then to create such objects on the fly (attribute names and data are set dynamically at runtime and can be changed later), you can use types.SimpleNamespace use . Related question: Python: create object and add attributes to it .

What is the difference between the two codes below?

Usually all attributes of an object should be specified in __init__ additional attributes should not be defined outside __init__ without special reason. The second example of the code in question is closer to SimpleNamespace (without some convenient methods), the first example of code in question is closer to namedtuple (without guarantees of immutability).

  • Thank! Is it acceptable to use SimpleNamespace like this many times? There will be a lot of people. from types import SimpleNamespace andrew = SimpleNamespace (sex = "Male", number = "12345") julia = SimpleNamespace (sex = "Female", number = "67890") - Newbie
  • @NickCaulfield: as I suggested, use csv and namedtuple in your case, for example, to read the csv file and save each person as the namedtuple shown above: people = [Person(["Male", "Female"][sex=="M"], tuple(numbers)) for sex, *numbers in csv.reader(open("people.csv"))] - jfs
  • Well thank you. And yet, I would like to know what about my question? Will everything be so bad if I do this with SimpleNamespace? - Newbie
  • @NickCaulfield can be driven with a screwdriver, but why. Different tools for different tasks may be better suited. Aside: error in my comments — you need == to != Replace. - jfs

To create classes and instances of these classes for storing data, I believe, is an overhead. Easier to use both in the python itself, and in general:

 SEX_MALE = 'Male' SEX_FEMALE = 'Female' andrew = {'sex': SEX_MALE, 'numbers': ('123 123 123', '345 345 345')} julia = {'sex': SEX_FEMALE, 'numbers': ('321 312 312', '654 654 645')} 

    Taking an object from an empty class and forming a structure for a specific object is a very bad option. Even if specifically for your case there will be no difference, then when you try to expand or change the code, you will come across a huge number of carefully laid out rakes.

    But even if you decide that you will not need to refine the script in the future, it will be needed only for the current task, the second option is fraught with a high probability of errors. Well, in general - you need to train yourself to always use the right decisions.

    Well, better data to initialize all these people is not hardcoded in the script, but save to a separate file, and initialize the class instances in a loop, reading the data from the file.

    • In general, the code on python is just a separate text file. It is very convenient to store data in the format of the python file. It is not by chance that Django stores the configuration in settings.py . - Nikmoon
    • @Nikmoon and nothing else but configuration and migrations :) - andmalmal
    • @Nikmoon, after all, it is better to use other file formats for storing a large number of records with a given set of fields - for example, CSV - Xander