473,387 Members | 1,528 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.

Scope actions

Hi,

I often write small classes in C++ the use the side effects of scope.
For example a CAutoLock class that locks a mutex in the constructor and
releases it in the destructor. Is this something that is easily
accomplished in Python? I'm guessing that using __init__ and __del__
would be roughly equivalent to this. But can can I be sure - with
garbage collection - that __del__ is called immediately at the point of
going out of scope?

Alternatively, should I give up this little idiom and use try / finally?
Thanks,

Will McGugan
Jul 18 '05 #1
2 1052
I often write small classes in C++ the use the side effects of scope.
For example a CAutoLock class that locks a mutex in the constructor and
releases it in the destructor. Is this something that is easily
accomplished in Python? I'm guessing that using __init__ and __del__
would be roughly equivalent to this. But can can I be sure - with
garbage collection - that __del__ is called immediately at the point of
going out of scope?
From what I have come to experience, adding a __del__ method is an
almost sure way of guaranteeing that the object will never be garbage
collected. This may not be the case in general, but every thing I've
tried to use it on has resulted in a memory leak; an infinitely growing
gc.garbage .

If you must do that kind of thing, play a bit with weakref objects. You
can make them work the way you want them to.

Alternatively, should I give up this little idiom and use try / finally?


I would make that suggestion. It isn't so bad to:

class foo:
def __init__(self):
self.lock = threading.Lock()
def accessor(self):
try:
self.lock.acquire()
#do work here
finally:
self.lock.release()
- Josiah

Jul 18 '05 #2
Will McGugan <ne**@NOwillmcguganSPAM.com> wrote:
...
Alternatively, should I give up this little idiom and use try / finally?


Alas, this is indeed the case. Wish it would be better...
Alex
Jul 18 '05 #3

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

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
1
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the...
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
3
by: Andrew Robinson | last post by:
I have never done this or seen this but thought it might be useful. What do you think of it? This is a very simplified example but frequenly when reusing an object, I find myself spending a lot of...
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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.