类变量会在python中自行重置为初始值吗?

时间:2018-11-20 22:28:49

标签: python-3.x oop python-3.7

当特定类的实例仍然存在时,类变量是否会自行重置? 我有一个类,在实例化对象的过程中,我在init中更新了类变量,以备将来使用时无法访问实例化的对象。我知道,当我尝试访问此类变量时,对象不会超出范围。示例代码段如下所示。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MoveObjects : MonoBehaviour
{
    public enum LineType
    {
        Curve, Straight
    }

    public List<GameObject> objectsToMove = new List<GameObject>();
    public AnimationCurve curve;
    public float stepsPerSecond = 1f;
    public bool changeDirection = false;
    //public LineType lineType;

    private Vector3 trackStart;
    private Vector3 trackEnd;
    private Vector3 horizontalTravel;
    private float verticalTravel;
    private float divisor;
    private float phase = 0f;

    // Use this for initialization
    public void Init()
    { // TO USE TAGS TO FIND THE STAIRS/STAIR BETWEEN SCRIPTS


        //lineType = LineType.Curve;

        if (curve == null)
        {
            curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
        }
        curve.preWrapMode = WrapMode.Clamp;
        curve.postWrapMode = WrapMode.Clamp;

        trackStart = objectsToMove[0].transform.position;
        int count = objectsToMove.Count;
        var span = objectsToMove[count - 1].transform.position - trackStart;

        divisor = 1f / count;
        horizontalTravel = (count + 1) * span * divisor;
        horizontalTravel.y = 0f;

        verticalTravel = span.y;
        trackEnd = trackStart + (count + 1) * span / count;
    }

    // Update is called once per frame
    void Update()
    {
        if (objectsToMove != null && objectsToMove.Count > 0 && curve != null)
        {
            AnimationCurve();
            /*curve = new AnimationCurve(curve.keys[0], curve.keys[1]);

            if (lineType == LineType.Straight)
            {
                StraightLineTrack();
            }

            else if (lineType == LineType.Curve)
            {
                AnimationCurve();
            }*/
        }
    }

    private void AnimationCurve()
    {
        phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);

        for (int i = 0; i < objectsToMove.Count; i++)
        {
            float t = Mathf.Repeat(phase + i * divisor, 1f);
            // Get the height of the curve at this step.
            float curveHeight = curve.Evaluate(t) * verticalTravel;

            if (changeDirection)
            {
                objectsToMove[i].transform.position = trackStart                // First step
                                  - horizontalTravel * t      // evenly spaced horizontal
                                  + curveHeight * Vector3.up; // curving vertical
            }
            else
            {
                objectsToMove[i].transform.position = trackStart                // First step
                                  + horizontalTravel * t      // evenly spaced horizontal
                                  + curveHeight * Vector3.up; // curving vertical
            }
        }
    }

    private void StraightLineTrack()
    {
        float divisor = 1f / objectsToMove.Count;

        // Compute the current phase of the escalator, 
        // from 0 (1st step at track start) to 1 (1st step at track end)
        phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);

        // Place each step a proportional distance along the track.
        for (int i = 0; i < objectsToMove.Count; i++)
        {
            float t = Mathf.Repeat(phase + i * divisor, 1f);
            objectsToMove[i].transform.position = Vector3.Lerp(trackStart, trackEnd, t);
        }
    }
}

我想在以后使用var(即“ John”)。当我到达那部分时,“ A.var”的值是“”,而不是我所期望的“ John”。完整的代码在此处发布很复杂。所以我刚刚提供了发生情况的基本情况

1 个答案:

答案 0 :(得分:0)

否。

您不是选择working example来让我们重现您看到的症状,而是选择提供可以按文档记录的方式工作而从不显示症状的代码,这使我们可以猜测您的情况。随信附上,您的代码稍长一些:

> typeof(dummyDF$Status)
[1] "list"

最大的不同是记录了空字符串分配,这似乎是您的主要关注点。毫不奇怪,产生的输出是:

def empty():
    print('empty')
    return ''


class A:
    var = empty()
    def __init__(self, name):
        self.name = name
        A.var     = name


obj_john = A('John')
print(A.var)

obj_mary = A('Mary')
print(A.var)

也就是说,空字符串只被分配了一次,然后ctor反复重写了单例。

如果您滥用模块的重复empty John Mary ,那么您可能设法两次调用空分配,但是您没有描述任何这些交互。设置调试器观察点可能会很有用。进一步挖掘,然后与我们分享您的发现。