473,549 Members | 2,758 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("som e 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 1342

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

Similar topics

1
8319
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 but that's the gist of it.) I have everything set up, and it seems to work except that setting Cancel to true and returning from my c# method appears...
1
7337
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 each line of a file, processes it , and writes it. With the Cancel button, I'd like to cancel this method. Is this possible? Thanks.
6
2229
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 C++ (changing to C# soon) and can create separate forms, but I really just want to clone the first one, have the user select different options on each...
14
2613
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 template which is not such a bad thing in itself but it means all other controls in the application must also be converted to templates to maintain a...
6
4948
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 event of a datatable, from two seperate classes. I wish to do some data validating. Due to the nature of the data validation being done, I seperated...
4
1480
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 returns a cross-threading error, since (it appears) the Splashform is loaded by a different thread. Any advice? I could ignore the SplashForm...
3
7646
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, How does the form subsequently know to NOT close the form? More generally, after an event is raised, does the event raising class somehow retain a...
16
2416
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 blocks (the server is executing somthing where which takes time)
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
7518
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...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
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...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
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
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.