Styx1337
Styx1337

Reputation: 37

Python Class with a lot of arguments

I am writing a program for my Vocabulary Trainer. Therefor I want to create a class for one entry.

But the class will get a lot of arguments, currently around 15. Is a Class really the right approach then?

class Voc_Entry:
def __init__(self, id_n, german, kanji_writing, kana_writing, word_type =  None, date_added = datatime.today(), kanji_write_progress = None, kanji_read_progress = None, kana_read_progress = None, kana_write_progress = None, kanji_write_last_date = None, kanji_read_last_date = None, kana_read_last_date = None, kana_write_last_date = None, kanji_write_next_date = None, kanji_read_next_date = None, kana_read_next_date = None, kana_write_next_date = None):
    self.id_n = id_n
    self.german = german
    self.kanji_writing = kanji_writing
    self.kana_writing = kana_writing
    self.word_type = word_type
    self.datetime = date_added
    .........

Or should I make divide that up into functions in the class to get the dates all afterwards? Thank you for your advice Styx1337

Upvotes: 0

Views: 416

Answers (1)

Samwise
Samwise

Reputation: 71454

My advice is to find ways to group the arguments into logical units. Seeing more of the context would make it easier to make concrete suggestions, but the kana... and kanji... groupings suggest that maybe there should be some sort of class to represent progress tracking?

from datetime import datetime
from typing import NamedTuple, Optional


class ProgressEntry(NamedTuple):
    read_progress: float
    write_progress: float
    read_last_date: datetime
    write_last_date: datetime
    read_next_date: datetime
    write_next_date: datetime


class VocabEntry:
    def __init__(
        self, 
        id_n, 
        german, 
        kanji_writing, 
        kana_writing, 
        word_type =  None, 
        date_added = datetime.today(), 
        kana_progress: Optional[ProgressEntry] = None,
        kanji_progress: Optional[ProgressEntry] = None, 
    ):
        self.id_n = id_n
        self.german = german
        self.kanji_writing = kanji_writing
        self.kana_writing = kana_writing
        self.word_type = word_type
        self.datetime = date_added
        ...

Upvotes: 3

Related Questions