简体   繁体   English

如何在不更改python的情况下访问类变量?

[英]How do I access class variables without changing them in python?

I'm new to programming so sorry for the basic question. 我是编程新手,所以很抱歉这个基本问题。 I am trying to write a search algorithm for a class, and I thought creating a class for each search node would be helpful. 我正在尝试为一个类编写搜索算法,并且我认为为每个搜索节点创建一个类会有所帮助。

class Node(object):

    def __init__(self, path_to_node, search_depth, current_state):
        self.path_to_node = path_to_node
        self.search_depth = search_depth
        self.current_state = current_state

    ...

With some functions too. 也有一些功能。 I am now trying to define a function outside of the class to create children nodes of a node and add them to a queue. 我现在正在尝试在类外部定义一个函数,以创建节点的子节点并将其添加到队列中。 node.current_state is a list node.current_state是一个列表

def bfs_expand(node, queuey, test_states):
    # Node Queue List -> Queue List
    # If legal move and not already in test states create and put children nodes
    # into the queue and their state into test_states. Return queue and test states

    # Copy original path, depth, and state to separate variables
    original_path = node.path_to_node
    original_depth = node.search_depth
    original_state = node.current_state

    # Check if up is legal, if so add new node to queue and state to test state
    if node.is_legal_move('Up'):
        up_state = original_state
        a = up_state.index(0)
        b = a - 3
        up_state[a], up_state[b] = up_state[b], up_state[a]
        if up_state not in test_states:
            test_states.append(up_state)
            up_node = Node(original_path + ['Up'], original_depth + 1, up_state)
            queuey.put(up_node)

    print(test_states)
    print(original_state)

I then try to proceed through down, left and right with similar if statements, but they are messed up because the original_state has changed. 然后,我尝试使用类似的if语句从下至上,从左至右进行操作,但由于original_state已更改,因此它们被弄乱了。 When I print the original state after that up statement, it returns the up_state created in the if statement. 当我在该up语句之后打印原始状态时,它返回在if语句中创建的up_state。 I realize (well, I think) that this is because original_state, and therefore up_state, are actually calling node.current_state and do not store the list in a separate variable. 我意识到(嗯,我认为)这是因为original_state和up_state实际上是在调用node.current_state,并且未将列表存储在单独的变量中。 How should I get the variable from a node to manipulate independently? 我应该如何从节点获取变量以进行独立操作? Should I not even be using a class for something like this, maybe a dictionary? 我是否应该甚至不使用此类的类,也许是字典? I don't need code written for me but a conceptual nudge would be greatly appreciated! 我不需要为我编写的代码,但是非常感谢您在概念上有所帮助!

You should use copy.deepcopy if you want to avoid modifying the original 如果要避免修改原始文件,则应使用copy.deepcopy

original_path = copy.deepcopy(node.path_to_node)
original_depth = copy.deepcopy(node.search_depth)
original_state = copy.deepcopy(node.current_state)

Or essentially whichever object you want to use as a "working copy" should be a deep copy of the original if you don't want to modify the original version of it. 或者基本上,如果您不想修改原始版本,则想要用作“工作副本”的任何对象都应该是原始版本的深层副本。

Expanding a bit on @CoryKramer's answer: In Python, objects have reference semantics , which means that saying 扩展@CoryKramer的答案:在Python中,对象具有引用语义 ,这意味着

a = b

where a and b are objects, makes both a and b references to the same object, meaning that changing a property on a will change that same property on b as well. 其中ab都是对象,使得无论ab引用同一个对象,这意味着在改变属性a将改变该相同属性上b为好。 In order to actually get a new object with the same properties as the old one, you should use copy.deepcopy as already stated. 为了实际获得与旧对象具有相同属性的新对象,应使用已经声明的copy.deepcopy However, be careful when using that function. 但是,使用该功能时要小心。 If your object contains a reference cycle (ie: It contains a reference to an object which contains a reference to itself), copy.deepcopy will lead to an infinite loop. 如果您的对象包含一个引用循环(即:它包含对对象的引用,该对象包含对自身的引用),则copy.deepcopy将导致无限循环。

For this reason, there is also copy.copy , which does not follow object references contained in the object to copy. 因此,还有copy.copy ,它不跟随要复制的对象中包含的对象引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Tkinter:如何避免在变化的类中使用全局变量? - Python Tkinter: How do I avoid global variables in a changing class? 如何在不更改变量的情况下在类和函数之间移动变量? - How do I move variables between Classes and functions without them changing? 在Python中,如何编写可以访问私有属性而不暴露它们的单元测试? - In Python, how do I write unit tests that can access private attributes without exposing them? 定义现有类变量时如何引用 - How do I reference existing class variables when defining them 如何在 Python 中访问环境变量? - How do I access environment variables in Python? 如何通过Python中子类的实例访问基类变量? - How do I access base class variables through an instance of a child class in Python? 如何替换python类中的变量? - How do I replace variables in python class? 如何使字符串和变量可编辑并打印它们? (Python) - How do I make strings and variables editable and printing them? (Python) 如何在不运行变量声明之外的所有代码的情况下从 Python 中的另一个文件访问变量? - How do I access variables from another file in Python without running all the code outside of the variable declaration? 如何同时运行两个不定循环,同时还要更改其中的变量? - How do I run two indefinite loops simultaneously, while also changing variables within them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM