473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1331

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

Similar topics

1
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.