Search code examples
pythonclassinheritanceclass-method

classmethod inheritance and overwrite class parameter


I struggle to understand how to assign a value to PersonProfile from a class method when PersonProfile inherits from PersonBase.

class PersonBase:
    def __init__(self, contact_no=None, email=None, house=None, category=None, near=None, house_number=None, road=None,
                 unit=None, level=None, staircase=None, entrance=None, po_box=None, postcode=None, suburb=None,
                 city_district=None, city=None, island=None, state_district=None, state=None, country_region=None,
                 country=None, world_region=None):
        self.contact_no = contact_no
        self.email = email
        self.house = house
        self.category = category
        self.near = near
        self.house_number = house_number
        self.road = road
        self.unit = unit
        self.level = level
        self.staircase = staircase
        self.entrance = entrance
        self.po_box = po_box
        self.postcode = postcode
        self.suburb = suburb
        self.city_district = city_district
        self.city = city
        self.island = island
        self.state_district = state_district
        self.state = state
        self.country_region = country_region
        self.country = country
        self.world_region = world_region



class PersonProfile(PersonBase):

    @classmethod
    def from_string(cls, full_address):
        """
        Takes raw, unformatted address string and creates a PersonProfile class out of it.
        :param full_address: unformatted address coming from a DB
        :type full_address: str
        :return: PersonProfile class
        :rtype: PersonProfile
        """

        if full_address == "":
            # return empty PersonProfile cls with all its defaults
            return cls(PersonProfile)
        elif full_address is None:
            raise TypeError("Provided object must be of type string")

        # extract phone numbers
        _contact_no = PersonProfile.find_phone_no(full_address)
        if len(_contact_no) != 0:
            cls.contact_no = ",".join(_contact_no)
            for c in _contact_no:
                full_address = full_address.replace(c, '')

        return cls()

So when I try to assign a phone number to PersonProfile.contact_no using cls.contact_no = ",".join(_contact_no) it doesn't seem to have any effect. What is a correct way to do so?

Also, at the end of that class method how would I return all values including the one I've overwritten during classmethod execution? I'm using return cls() but doesn't seem to be working either.

I'd like to make it work like so:

p1 = PersonProfile.from_string("(+22) 936107349")
print(p1.contact_no)
--> (+22) 936107349
print(p1.city)
--> None

Solution

  • You want to provide the parsed data to the __init__ method; by calling cls(**key_word_arguments), the dictionary of kwargs returned by the staticmethod parse_full_address is passed the constructor.

    I suggest that you write a parser, and validators for the data separate from the class factory, and pass to the factory arguments that have been thoroughly extracted.

    class PersonProfile(PersonBase):
    
        @classmethod
        def from_string(cls, full_address):
            """
            Takes raw, unformatted address string and creates a PersonProfile class out of it.
            :param full_address: unformatted address coming from a DB
            :type full_address: str
            :return: PersonProfile class
            :rtype: PersonProfile
            """
            kwargs = PersonProfile.parse_full_address(full_address)
    
            return cls(**kwargs)
        
        @staticmethod
        def parse_full_address(full_address):
            """parses the full_address and returns the parsed data as a dictionary
            currently a stub that returns test data
            """
            return {'contact_no': '(+22) 936107349', 'email': 'a@a.com', 'house': 'Ze Mansion', 'city': 'Dummy Data'}
        
        
    p1 = PersonProfile.from_string("(+22) 936107349")
    print(p1.contact_no)
    print(p1.city)