dimanche 1 mars 2020

What is the proper way of updating class-fields with class-methods?

I often find myself in a situation that I can do either of the two scenarios:

class Foo:
  def __init__(self):
    self.a: str = ''
    self.my_method()

  def my_method(self):
    self.a = ... # some magic that finds the actual value of a

or

class Foo:
  def __init__(self):
    self.a: str = self.my_method()

  def my_method(self):
    a = ... # some magic that finds the actual value of a
    return a

Question: What is the right design choice? (I don't find any of them smart!)


Note: I don't think it is a good idea to turn this method into a function (i.e., taking my_method outside of the class), as I believe it conceptually belongs to that class.

Aucun commentaire:

Enregistrer un commentaire