Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class member become static
#1
In the following code data must be an non static attribute of the class Node:

class Node:
    total = 0
    def __init__(self,parent,data):
        Node.total=Node.total+1
        self.index=Node.total
        self.data = data
        if self.index!=1:
            self.parent = parent.index
            self.depth=parent.depth+1
        else:
            self.parent=1
            self.depth=0

list=[Node(0,[2,2,2,2,2,2])]
i=0
while list[i].depth<2:
    for j in range(0,6):
        p_data=list[i].data
        num=p_data[j]
        if num!=0:           
            p_data[j]=0
            for k in range(1,num+1):
                p_data[(j+k)%6]=p_data[(j+k)%6]+1
            list.append(Node(list[i],p_data))
    i=i+1
       
For every class instance in list all member have the same value for member data and also become static.
Reply
#2
Do not use "list" as a variable name. list() is a built-in function that creates a list.

There are no "static" variables in Python. "total" is a class variable.

Your problem is here:
        p_data=list[i].data
p_data and list[1].data are the same list object. Even though "data" is an instance variable that references a list, all instances of Node end up referencing the same list object. Do you want each node to have their own list? You could do this:
        p_data=list[i].data.copy()
Now p_data and list[1].data are different lists. They have the same contents, but they are different list objects. When you create a new Node using pdata, the data in the old Node and the new Node will be different lists.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem usage of static method akbarza 5 557 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Static type checking with PyPy pitosalas 1 465 Dec-14-2023, 09:29 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,951 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,937 Dec-15-2021, 01:53 PM
Last Post: deanhystad
Photo Output Static image on HDMI2 random816382 0 1,412 Oct-18-2021, 11:21 AM
Last Post: random816382
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,670 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Syntax to print ndarray member of class containing ndarray pjfarley3 6 3,257 Jul-09-2020, 06:29 AM
Last Post: pjfarley3
  Instance of 'socket' has no 'error' member fedex03 1 2,904 May-13-2020, 03:23 PM
Last Post: deanhystad
  Are there optimizer for static code? AlekseyPython 5 2,594 Nov-03-2019, 07:52 AM
Last Post: AlekseyPython
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 1,845 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020