0

I have below datamemebers in the class and method that overload with same data members

What i am looking forward is to validate the name of members from class against the overloaded members

class Payload:
    data_members=['id','name','acccount']

    def __init__(self):
        self.id=''
        self.name=''
        self.account=''



    def add_data(self, id=None,name=None, account=None):
        print(self.data_members)
        overload_members=[id,name,account]

        ## what i am looking for is
        for varibale1_name in self.data_members:
            for variable_name2 in overload_members:
            if varibale1_name==variable_name2:
               print("varibale1_name and variable_name2 are same, printing same variable name:",varibale1_name, variable_name2) 
            else:
               print("varibale1_name and variable_name2 are not same, printing non matched variables: ",varibale1_name, variable_name2)
        

        


id=1
name='Jakson'
account=123

obj= Payload
obj.add_data(id,name,account)

so the out put should like this

#Expected
varibale1_name and variable_name2 are same, printing same variable name: id,name,account

if overload with different variable name against datamembers

for eg:

obj.add_data(id,name,age)

#output is
varibale1_name and variable_name2 are not same, printing non matched variables: variable name :age

so the expected result should return the 'variable name' not the value of variables

is there any optimized way to achieve in python?

Appreciated for the help..!

Thanks

Cloud_Hari
  • 49
  • 1
  • 5
  • You can use a [list comprehension](https://www.programiz.com/python-programming/list-comprehension) and [join](https://www.w3schools.com/python/ref_string_join.asp). Any case: `args = [arg for arg in overload_members if arg not in self.data_members]` and `print(f"Overloaded args are: {', '.join(args)}")`. – Thymen Dec 18 '20 at 08:54
  • HI @Thymen, i understood the logic to implement the validation, but my question is how to get the 'variable name' from the data members to validate.. not the 'value of varaible' by the way i will modify my question with expected result.. please have a look – Cloud_Hari Dec 20 '20 at 14:04
  • Then you are looking for [type checking](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) ? Your question is not clear to me. If it is type checking you can use `isinstance(type(var1, var2))`, if you want to allow more members you can use `**kwargs`, and then make sure to only pick names that are in the data members. Your example seems like checking on name, which is only possible when using `key word arguments`, not normal arguments. For type checking see first link, please update your question. – Thymen Dec 21 '20 at 07:20

0 Answers0