473,513 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Cancel instance create



Fredrik Lundh wrote:
Aigars Aigars wrote:
>I want MyClass to perform some tests and if them fail, I do not want
instance to be created.
If you do not want the instance created at all, you would have to write
a custom .__new__ method, but that is tricky, something I would have to
look up how to do, and most likely not needed. Fredrik's solution below
is much easier and the one I would use if at all possible.
>But in code I wrote instance is created and also has parameters, that
it should not have in case of tests failure.

Is there a way to perform tests in MyClass.__init__ and set instance
to None without any parameters?

if you want construction to fail, raise an exception.

...

def __init__(self):
At this point, you have an instance of your class bound to local name
'self'. Usually, its only individual attributes are (in 3.0, anyway, as
far as I can tell) .__class__ and an empty .__dict__. Of course, it
inherits class and superclass attributes, but it is otherwise blank.
(The main exception would be if you were inheriting from an immutable
class that set attributes in .__new__, but then you would not be writing
..__init__.)
self.param = "spam"
Test = False
if Test: # please don't use explicit tests for truth
print "Creating instance..."
else:
raise ValueError("some condition failed")

(pick an exception class that matches the actual error, or create your
own class if necessary)
Once the exception gets disposed of (if not before), the blank instance
gets unbound from 'self' and since it does not get bound to anything
else, it becomes eligible for garbage collection.

tjr

Sep 6 '08 #1
0 1340

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

Similar topics

1
8301
by: AP | last post by:
Hi, I'm trying to use c# to pop up a dialog box when a user attempts to close word to prompt them if they want to exit or cancel (obviously other stuff needs to happen based on their selection...
1
7335
by: VM | last post by:
How can I cancel all running processes in my application when I click on the Cancel button? When the Run button's clicked, it creates an instance of a specified class and runs a method that reads...
6
2223
by: Patrick Coghlan | last post by:
I want to create about 4 forms with the same dimensions and background colours, similar to the forms one has to traverse when installing various software packages. I'm using Visual Studio and...
14
2608
by: clintonG | last post by:
This is an appeal for peer support sent to Microsoft as will be noted in closing. The Login control does not include a Cancel button. The only option is to convert the Login control to a...
6
4944
by: Peter M. | last post by:
Hi all, If an event has multiple subscribers, is it possible to cancel the invocation of event handlers from an event handler? Or to be more specific: I'm subscribing to the ColumnChanging...
4
1478
by: Graham Charles | last post by:
Hello, I'm finding that if I set the "Cancel" property during the AppStartup event, the application's Splash form remains loaded. A call to the Close property of the SplashForm at that point...
3
7644
by: Smithers | last post by:
In consideration of the brief sample code at the following link... http://msdn2.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx .... when we set e.Cancel = true,...
16
2414
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
0
170
by: Fredrik Lundh | last post by:
Aigars Aigars wrote: if you want construction to fail, raise an exception. ... def __init__(self): self.param = "spam" Test = False
0
7265
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,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7547
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5098
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...
0
3240
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...
0
3230
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1607
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 ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
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...

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.