Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

python 3.x - How to define a function to automatically copy a class and delete them without input argument?

There's a python module module1.py, inside this module there was a class1, and a variable defined as x=class1() and y=class1(). After several operation the object x contained data.

How to write a function clean_up() in module1 such that

module1.clean_up() #takes no input argument

would reset x into the empty class1() and make y contain the value of x? I have tried to use

def clean_up():
    y=copy(x);
    del x;
    x=class1();

However, this does not make a copy of x and pass it to y, nor does it run successfully and clear x. An error returned as

module1.clean_up()
UnboundLocalError: local variable 'Hecke_variable' referenced before assignment

i.e. Both x and y were treated as local variables. But in a previous post, Python class instance changed during local function variable, class was mutable and any changes to a class instance inside a function would reflect outside the function as well.

Why wasn't the function clean_up() run successfully? How to write clean_up()?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...