Home > Software engineering >  Is it possible in Python to call a child from parent class without initialize the child?
Is it possible in Python to call a child from parent class without initialize the child?

Time:12-04

I want to know if is it possible to create a parent class to handle some common logic, but have some specific logic in child classes and run it without initialize the child as it's in abstraction.

For example:

class Person:
  def __init__(self, fname, lname, country):
    self.firstname = fname
    self.lastname = lname
    self.country = country

    if country == "US":
      # Call class UnitedStates(Person)
    else if country == "CA":
      # Call class Canada(Person)

  def printCountry(self):
    print(self.firstname   " "   self.lastname   " is from "   self.country)

class UnitedStates(Person):
  def __init__(self):
    super().country="United States"
    pass

class Canada(Person):
  def __init__(self):
    super().country="Canada"
    pass

x = Person("John", "Doe", "US")
x.printCountry()

y = Person("Jane", "Doe", "CA")
y.printCountry()

So in x I have "John Doe is from United States" and in y I have "Jane Doe is from Canada".

The reason I need that come from a high complex logic and that's the easiest way to deal, so that sample is a dummy version of what I need, otherwise I'll need to find the best way for a "work around".

Thanks in advance.

CodePudding user response:

Yes, it is possible to call a child class method from a parent class without initializing the child class in Python. However, it's important to note that you would not be able to call the child class method directly in the parent class.

In order to call a child class method from a parent class, you can use the super() function, which allows you to access the methods and attributes of the parent class. Then, you can use the isinstance() function to check the type of the object and call the appropriate child class method.

Here is an example of how you could accomplish this:

class Person:
  def __init__(self, fname, lname, country):
    self.firstname = fname
    self.lastname = lname
    self.country = country

    if isinstance(self, UnitedStates):
      self.printCountry()
    elif isinstance(self, Canada):
      self.printCountry()

  def printCountry(self):
    print(self.firstname   " "   self.lastname   " is from "   self.country)

class UnitedStates(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname, "United States")

class Canada(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname, "Canada")

x = UnitedStates("John", "Doe")
# Output: John Doe is from United States

y = Canada("Jane", "Doe")
# Output: Jane Doe is from Canada

In this example, the Person class has a printCountry() method that prints the person's name and country. The UnitedStates and Canada child classes inherit from the Person class and override the __init__() method to set the appropriate country name.

In the Person class, the __init__() method uses the isinstance() function to check the type of the object and calls the printCountry() method of the appropriate child class. This allows you to call the child class method without initializing the child class directly in the parent class.

CodePudding user response:

Yes, it is possible to create a parent class that has some common logic and child classes that have specific logic, and to call the child class methods without initializing an instance of the child class. However, the code you have provided will not work as you expect it to because it contains some errors and logical issues.

Here is one way you could modify your code to achieve the behavior you want:

class Person:
  def __init__(self, fname, lname, country):
    self.firstname = fname
    self.lastname = lname
    self.country = country

    # Call the appropriate child class based on the country
    if country == "US":
      self.us = UnitedStates()
    else if country == "CA":
      self.ca = Canada()

  def printCountry(self):
    print(self.firstname   " "   self.lastname   " is from "   self.country)

class UnitedStates(Person):
  def __init__(self):
    super().__init__()
    self.country = "United States"

class Canada(Person):
  def __init__(self):
    super().__init__()
    self.country = "Canada"

# Create an instance of the Person class and call the printCountry method
x = Person("John", "Doe", "US")
x.printCountry()

# Create another instance of the Person class and call the printCountry method
y = Person("Jane", "Doe", "CA")
y.printCountry()

In this code, the Person class has a constructor method that takes three arguments: the first name, last name, and country of a person. Based on the country, it creates an instance of the appropriate child class (UnitedStates or Canada) and assigns it to the us or ca attribute of the Person instance. The printCountry method of the Person class then prints a message using the firstname, lastname, and country attributes of the Person instance.

The UnitedStates and Canada child classes each have a constructor method that calls the init method of the parent Person class, and then sets the country attribute to the appropriate value.

When you create an instance of the Person class and call the printCountry method, the appropriate child class is called and the country attribute is set to the correct value before the message is printed.

  • Related