473,946 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destroy your self????

if I create an object like...

obj = None
....
obj = anObject()

can obj set itself to none in some method of the class?

Oct 20 '05 #1
7 1975
KraftDiner wrote:
if I create an object like...

obj = None
...
obj = anObject()

can obj set itself to none in some method of the class?


No - Python doesn't work that way.

What are you trying to accomplish? There's probably a way to do what you need to
do, but this isn't it.

-Dave
Oct 20 '05 #2
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)

Oct 20 '05 #3
KraftDiner wrote:
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)


In Python, variables don't "hold" values - variables are just named references
to objects:

x = 5 # x refers to the 5 integer object
x = 'hi' # now x refers to the string object

By reassigning a name to something else, that's all you're doing - ever other
reference to that object will still be intact.

If you were able to flag the degenerate case, code that uses the object would
have to check for it, right? That being the case, the same code could just as
easily test a member function.

Better yet, if you can catch this case at the point when the object is created,
just throw an exception or at least perform the test at that point only.
Oct 20 '05 #4
KraftDiner wrote:
if I create an object like...

obj = None
...
obj = anObject()

can obj set itself to none in some method of the class?

Do you mean like this?

def foo(): .... global foo
.... del foo
.... foo()
foo

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'foo' is not defined
Cheers,
Ron
Oct 20 '05 #5
"KraftDiner " <bo*******@yaho o.com> writes:
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None :)


You're running headlong into the fact that Python's variables are just
ways to reference objects - "names" if you will - and not objects
themselves. Whe you write "foo = some_obj", all you're doing is making
tha name "foo" refer to some_obj instead of to whatever it was
referring to before. Neither some_obj nor the object previously known
as "foo" are changed in anyway.

To make a change in the object, you have to make a change in the
object proper, not just a name that refers to it. Adding a flag that
clients can test to see if the object is still valid would do
it. Changing the operations that clients do on the object so they
raise exceptions will also work, and might be considered more
Pythonic.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 20 '05 #6
Doesn't work for classes because self has no global reference.

Oct 20 '05 #7
James wrote:
Doesn't work for classes because self has no global reference.


True. To make it work one would need to track instances and names and
do comparisons... and so on. So it's not worth it. ;-)

Cheers,
Ron
Oct 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
26293
by: Rob | last post by:
My first GUI so be gentle... When I start my program I call a class that runs the initial window. While in this class if a certain button is pressed it calls a function outside the class. This function then initially calls another function to "root.destroy()". Basically I want the current window gone so the function I just called can open it's own window. The problem I'm stuck with is that once this function is done and I need to close...
1
2288
by: Minkyu Kim | last post by:
Hi. Please check this simple test code. --------------------- class TestA: def __init__(self): print "init TestA" def __del__(self): print "del TestA" def SetEvent(self, event):
6
18026
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
2
6294
by: Ook | last post by:
I was taught that in a copy constructor, you don't have to destroy your arrays, but in an overloaded assignment operator, you have to. Example: When do you delete, and when do you not? Is it arbitrary, or are there general guidelines that should be followed? I'm thinking that in the copy constructor, you are creating a new instance of the class, and in the assignment, you have already created the class and therefore have to destroy...
8
1958
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
2
1559
by: Flavio | last post by:
Hi, I have a miniframe composed mainly of combo boxes, that I need to destroy and recreate multiple time with different choice lists for the combo boxes. My problem is that even after destroying a miniframe with the Destroy() method, when it is recreated, the combo boxes show the same lists of its previous incarnation...
6
4040
by: muppetjones | last post by:
I'm pretty new at this, and I'm trying to figure out how Perl's classes work with signals. Specifically, it doesn't seem that a class's DESTROY function is called when you Ctrl-C the program. I tried using use sigtrap qw(handler DESTROY INT QUIT);, but I'm not even sure this is the proper way to catch the signal. Either way, it seems I no longer receive a reference to my object when DESTROY is called. I keep getting this error: Can't...
4
2954
by: Curious | last post by:
Hi, I have a timer object that's launched as below: mTimer = new System.Threading.Timer (new TimerCallBack(SubscribeTrade), null, 15000, 15000); void SubscribeTrade (object state) { DateTime now = DateTime.Now;
3
6534
by: drzoo2 | last post by:
Completely noob question as I am not a programmer but really trying hard to learn Python (Object oriented programming in general). I am writing a program in python that calls a popup window with some general information with an ok button. If I close the window using the window's close button I have no problems but If I call the same destroy function using the button call, it will not kill the popup. def destroy(self, widget, data=None):...
0
10150
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10679
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6097
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4928
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3526
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.