Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...