update fct in python constructors -


i have come across in "artificial intelligence: modern approach" code repository following piece of code have never seen before:

  def __init__(self, state, parent=none, action=none, path_cost=0):     "create search tree node, derived parent action."     update(self, state=state, parent=parent, action=action,            path_cost=path_cost, depth=0)     if parent:         self.depth = parent.depth + 1 

they seem using update function redefine parameters of constructor, allow alternative arguments. looked everywhere in code , not find self-defined function named update. allowed in python? couldn't find online.

that not 1 of python's built-in functions, it's not defined locally or listed in:

import math, random, sys, time, bisect, string 

(ulgh!) must have come other import in file:

from utils import * 

(this why the style guide says "wildcard imports should avoided, make unclear names present in namespace"...).


checking in that file find:

def update(x, **entries):     """update dict; or object slots; according entries.     >>> update({'a': 1}, a=10, b=20)     {'a': 10, 'b': 20}     >>> update(struct(a=1), a=10, b=20)     struct(a=10, b=20)     """     if isinstance(x, dict):         x.update(entries)     else:         x.__dict__.update(entries)     return x 

the use of function simplifies otherwise like:

def __init__(self, state, parent=none, action=none, path_cost=0):     "create search tree node, derived parent action."     self.state = state     self.parent = parent     self.action = action     self.path_cost = path_cost     self.depth = 0     if parent:         self.depth = parent.depth + 1 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -