Is it possible to call a Setter inside a python class method itself?

Asked

Viewed 55 times

0

I have a class that reads an XML file and with the file information fills the class attributes. All the data consistency checks I do within the "setters" of the attributes (the class can be used without the input file, directly by another code and so I don’t need to repeat the checks on other parts of the code).

For example, I have Setter:

 @fluid_list.setter
    def fluid_list(self, fluid_list: []):
        for i in fluid_list:
            self.__fluid_list.append(fluid_list[i].upper())
        assert (len(fluid_list) == len(set(fluid_list)))
        assert (len(np.setdiff1d(fluid_list,["OIL", "WATER", "GAS"])) == 0)

And I have the method

def read_file(self):

        try:

            # parse an xml file by name
            mydoc = minidom.parse(self.__input_file_name)


            # reads function elements to get all function names (FunctionList)
            list_elem = mydoc.getElementsByTagName('fluid')  
            fluid_l = []
            for elem in list_elem:
                fluid_l.append(elem.firstChild.data)
            self.fluid_list(fluid_l)    
            print(self.__fluid_list)


        except Exception as e:
            logger.catch_error(e)

While running I get the following error message:

File "...prj\InputData.py", line 158, in read_file self.fluid_list(fluid_l)
TypeError: 'list' object is not callable  

Is there any way to call Setter within the class, or need to include consistency checking in the method that reads the input file?

  • self.fluid_list(fluid_l), that should be self.fluid_list = fluid_l if the intention is to call the Setter of your property.

  • 1

    And make the parameter fluid_list: [] seems to make no sense. In this case the [] will be a type note and it would not make much sense to be []. If the idea was to set a default value, it should be fluid_list = [], but you still need to be very careful with When a default argument is evaluated in Python

  • Yes, that’s the intention and it worked perfectly. My first implementation was this way, but there must have been something wrong elsewhere in the code because it hadn’t worked... Thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.