-2

I want to create two __init__s inside the python class, that would instantiate different elements of that class.

The question was asked because I was trying to implement a partial update to a record inside the database and I was using SQLAlchemy ORM which maps python class and its attributes to database Table and column respectively. However, I was to update a user_score column in a User's table then I create an instance of the User object inside the update function that listens to the PATCH request but it wasn't updating. So, I thought I could create another init that will only be responsible for handling only that attribute user_score.

class User(db.Model):
  __tablename__='users'

  id = Column(Integer, primary_key=True)
  name = Column(String, nullable=False)
  score = Column(Integer, nullable=False)

  def __init__(self, name, score):
    self.name = name
    self.score = score

  def __init__(self, score):
    self.score = score

  def insert(self):
    db.session.add(self)
    db.session.commit()
  
  def update(self):
    db.session.commit()

  def format(self):
    return {
      'id': self.id,
      'name': self.name,
      'score': self.score
    }
   
P. Son
  • 35
  • 6
  • How would it know which one to use? – Barmar Aug 24 '22 at 17:10
  • 3
    You really need to explain what you're trying achieve, possibly with an example, before this becomes answerable. – Mad Physicist Aug 24 '22 at 17:13
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 24 '22 at 18:52
  • This question was already asked on stackoverflow, https://stackoverflow.com/questions/3134829/can-i-have-two-init-functions-in-a-python-class , but ... there is still missing a very clear answer explaining why it is not possible to have two __init__() functions in a class. – Claudio Aug 28 '22 at 09:11
  • @Barmar you define the other version with another type/number of parameters so they could be distinguished. – Claudio Aug 28 '22 at 09:13
  • 1
    @Claudio Python doesn't have function overloading. A name can only have one definition. – Barmar Aug 28 '22 at 20:00

1 Answers1

1

Only one __init__ is possible, but you can have it do different things like:

def __init__(self, optA: bool = False, optB: int = 0):
    self.optA = optA
    self.optB = optB
    if self.optA:
        # do something different if optA is true (i.e., not the default)
    if self.optB != 0:
        # do something different if optB is not the default of 0
    etc...

So by giving sensible defaults to optional parameters, you can have a an __init__ whose behavior differs based on what parameters are supplied, and what their values are if supplied. Lots of good references on this out there on the internet :)

So, if you just instantiate the object with no parameters, you get the defaults as shown, but if you do supply them, then the values you supply override the defaults and trigger the behavior in the if statements inside.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • So, it's not possible to have more than one init inside Python class. But as you @TomServo said, it can be controlled by have its parameters initially assigned values. Alright – P. Son Aug 24 '22 at 17:30
  • It works for the intended purpose. – P. Son Aug 24 '22 at 17:41