1

I'm coding a decision tree and I decided a nice implementation would be a Class/Object structure of the branches (at least in C++). For the sake of simplicity I'm gonna break it down to the necessary parts:

class DecisionTreeBranch:
    def __init__(self):
         parent      = None
         leftBranch  = None
         rightBranch = None

where parent declares the way to the super decisions and leftBranch, rightBranch defines the decisions below.

What I'm trying to do:

Now going downwards ain't a problem, but moving upwards: Let's say this tree is defined by the left branches first, so if I end up in a final leaf. Im going one step above and take the right branch and continue with the tree building (if I go above, right and left is defined, I'm going another step above, and so on).

The aim is to iterate through the tree by the three parameters: parent (up) and leftBranch, rightBranch (down)

The problem

If I'd take e.g. C++ then this wouldn't be a problem, I'd define parent with a call-by-reference. But I recently started programming classes in Python, unfortunately I always end up in a call-by-value expression, where for sure all the information of the above branches are lost.

Basically I'm looking for something like:

[...]
# define way to former branch 
&curDecision.parent = &oldDecision
oldDecision = curDecision

# continue with the next/new decision on the current level
curDecision = DecisionTreeBranch()
[...]

Can anyone help me out ?

Kind Regards

user3085931
  • 1,757
  • 4
  • 29
  • 55
  • 1
    Possible duplicate of [How do I pass a variable by reference?](http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – John Bollinger Dec 28 '15 at 17:49
  • What does your python code actually look like? It's not clear how you end up with the problem you are describing. – pvg Dec 28 '15 at 17:51
  • @pvg well prettymuch like: `curDecision.parent = oldDecision` and `oldDecision = curDecision` – user3085931 Dec 28 '15 at 17:54
  • 2
    In Python, you never handle class instances other than by references to them (it is similar to Java in this respect). When you pass one of these references to a function, the reference is passed by value. The function can manipulate the referred-to object via the reference, but it cannot modify the caller's copy of the reference. – John Bollinger Dec 28 '15 at 17:55
  • In what way does that not work? In python those would be references to instances, assuming these are instances. – pvg Dec 28 '15 at 17:56
  • @pvg are you sure? Well then my code seems to have a bug. Right now I only can go 1 level upwards - that's why I'm guessing it's call-by-value in this case – user3085931 Dec 28 '15 at 17:59
  • 1
    I am quite sure. All instances in python are heap allocated. When you assign one to a variable, the variable ends up being a name for a reference to the instance. – pvg Dec 28 '15 at 18:01

2 Answers2

2

The following is the example of call by reference. You can also see that as multiple constructors cannot be used in python, so Keyword arguments are used in the init method:

class Maximum:
    def __init__(self,x = None,y = None):
        self.x = x
        self.y = y

    def max(self,Maximum):
        if Maximum.x > Maximum.y:
            print('{0} is maximum.'.format(Maximum.x))
        else:
            print('{0} is maximum.'.format(Maximum.y))

m1 = Maximum()
m2 = Maximum(3,8)
m1.max(m2)
1

You can always pass a dictionary with keys parent, left, right. You will be able to modify the dictionary and access the structure by keys.

Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
  • I guess dictionary implementation is something different than class/objects (?) – user3085931 Dec 28 '15 at 17:56
  • 1
    You can always pass a list of 1 element with your object in it. Then you can continue to use the class objects. – Robert Jacobs Dec 28 '15 at 17:59
  • @user3085931, not at all -- or at least not in any way that makes a difference to your question. In the scheme RobertJacobs proposes, a reference to the dictionary would be passed by value. The called function could modify its contents, which modifications would be visible to the caller, but it could not cause the caller's reference to point to a different dictionary. – John Bollinger Dec 28 '15 at 17:59