Search code examples
pythonparametersdefault-value

how to determine weather the default parameter values are used in the function?


there are two functions: init and set_info. (full visison shown at the end)

class DefaultNode(object):

    def __init__(self, node_name, node_pre= None, node_next = None, 
act_func='sign', agg_func='sum', bias=0.0, response=1.0, node_type=None):
    ...

    def set_info(self, node_pre= None, node_next = None, act_func='sign', 
agg_func='sum', bias=0.0, response=1.0):
    ...

Set_info function has many default parameter values. I don't want to rewrite those parameters, which were given in the initializing process.In other words, if one of the parameter is to be set using set_info, the other parameters should remain the same.

For example,

 temp_node = DefaultNode("givenname", node_pre = PRE) 
 temp_node.set_info("changedname")

the "node_pre" parameter of temp_node shall be "PRE" as initialized, instead of being replaced by the Default parameter in the function "set_info", i.e.

node_temp.node_pre != sign

the question is simple: how to implement it?

Thanks~

class DefaultNode(object):
    def __init__(self, node_name, node_pre= None, node_next = None, act_func='sign', agg_func='sum', bias=0.0, response=1.0, node_type=None):
        self.node_name = node_name
        self.node_pre = node_pre
        self.node_next = node_next
        self.act_func_name = act_func
        self.agg_func_name = agg_func
        if act_func == "sign":
            self.act_func = signmus_activation()

        if agg_func == 'sum':   # sign 和sum 是作为一个初始标记使用
            self.agg_func = sum
        self.bias = bias
        self.response = response
        self.node_type = node_type  

    def set_info(self, node_pre= None, node_next = None, act_func='sign', agg_func='sum', bias=0.0, response=1.0):
        self.node_pre = node_pre
        self.node_next = node_next        
        self.act_func_name = act_func
        self.agg_func_name = agg_func
        if act_func == "sign":
            self.act_func = signmus_activation()
        if agg_func == "sum":
            self.agg_func = sum
        self.bias = bias
        self.response = response

Solution

  • EDIT 2: Updated my response after you clarified your question further (thanks!).

    Option 1: You can use **kwargs and setattr to keep the parameter lists in sync. For example,

    class Example(object):
    
        def __init__(self):
            self.first = 1
            self.second = 2
    
        def set_info(self, **kwargs):
            for argument, value in kwargs.items():
                if not hasattr(self, argument):
                    raise ValueError('invalid argument {}'.format(argument))
                setattr(self, argument, value)
    
    
    # Usage
    e = Example()
    print(e.first, e.second)
    e.set_info(first=1000, second=2000)
    print(e.first, e.second)
    
    # This would raise an exception
    # e.set_info(some_random_attribute='blah')
    

    This outputs:

    1 2
    1000 2000
    

    Option 2: Instead of having a separate method for setting fields, you could use @property and a corresponding setter for each. For example,

    class Properties(object):
    
        def __init__(self):
            self._thing = 10
    
        @property
        def thing(self):
            return self._thing
    
        @thing.setter
        def thing(self, t):
            self._thing = t
    
    
    # Usage
    p = Properties()
    print(p.thing)
    p.thing = 20
    print(p.thing)
    

    Which prints

    10
    20