472,961 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to control the creation of an instance?

Hi,

suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.

any way to do this?

I do some work as follows:

class Sample(object):
used_object = []
free_object = []

def __init__(self, *args, **kwds):
pass
def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj

######## still get a new instance :(
def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True

Jun 3 '07 #1
7 1097
On Jun 2, 10:31 pm, lialie <lia...@gmail.comwrote:
Hi,

suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.

any way to do this?

I do some work as follows:

class Sample(object):
used_object = []
free_object = []

def __init__(self, *args, **kwds):
pass

def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj

######## still get a new instance :(

def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True
Try something like this:

class A(object):
pass

def getObj(alist):
if alist:
return alist.pop()
else:
return A()

x = [A(), A()]
print x

newlist = []
for i in range(0,3):
newlist.append(getObj(x) )

print newlist

--output:--
[<__main__.A object at 0x55bf0>, <__main__.A object at 0x55b50>]
[<__main__.A object at 0x55b50>, <__main__.A object at 0x55bf0>,
<__main__.A object at 0x55d30>]
Examine the addresses, i.e. the set of numbers in each <....>

Jun 3 '07 #2
I just noticed that in your code you used pop(0). It's not efficient
to pop an element off the front of a list because after doing so,
every other element in the list has to be moved over to the left one
position. If you pop() an element off the end of the list, then the
first element in the list remains the first element, and the 2nd
element remains the 2nd element, etc., so no shifting of the remaining
elements is required. If you need to pop elements of the front of a
list, then it's more efficient to use a deque, which can be found in
the collections module. Here is an example:

import collections

class A(object):
pass

def getObj(a_deque):
if a_deque:
return a_deque.popleft()
else:
return A()

x = collections.deque([A(), A()])
print x

newlist = []
for i in range(0,3):
newlist.append(getObj(x) )

print newlist

--output:--
deque([<__main__.A object at 0x55d90>, <__main__.A object at
0x55db0>])
[<__main__.A object at 0x55d90>, <__main__.A object at 0x55db0>,
<__main__.A object at 0x55e30>]

Jun 3 '07 #3
On Jun 2, 10:31 pm, lialie <lia...@gmail.comwrote:
Hi,

suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.

any way to do this?

I do some work as follows:

class Sample(object):
used_object = []
free_object = []

def __init__(self, *args, **kwds):
pass

def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj

######## still get a new instance :(

def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True
This seems to work for me:

import collections

class Sample(object):

free_objects = collections.deque()
used_objects = []

def __new__(cls, *args, **kwds):
if not Sample.free_objects:
temp = object.__new__(Sample, args, kwds)
Sample.used_objects.append(temp)
return temp
else:
return Sample.free_objects.popleft()

def __init__(self, *args, **kwds):
self.args = args
self.kwds = kwds

def release(self):
Sample.used_objects.remove(self)
Sample.free_objects.append(self)

s1 = Sample(10, name="Bob")
print s1
print s1.args
print s1.kwds

s2 = Sample("red", name="Bill")
print s2
print s2.args
print s2.kwds

s1.release()
s3 = Sample("blue", name="Tim")
print s3
print s3.args
print s3.kwds

Jun 3 '07 #4
On Jun 3, 12:17 am, 7stud <bbxx789_0...@yahoo.comwrote:
On Jun 2, 10:31 pm, lialie <lia...@gmail.comwrote:
Hi,
suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.
any way to do this?
I do some work as follows:
class Sample(object):
used_object = []
free_object = []
def __init__(self, *args, **kwds):
pass
def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj
######## still get a new instance :(
def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True

This seems to work for me:

import collections

class Sample(object):

free_objects = collections.deque()
used_objects = []

def __new__(cls, *args, **kwds):
if not Sample.free_objects:
temp = object.__new__(Sample, args, kwds)
Sample.used_objects.append(temp)
return temp
else:
return Sample.free_objects.popleft()

def __init__(self, *args, **kwds):
self.args = args
self.kwds = kwds

def release(self):
Sample.used_objects.remove(self)
Sample.free_objects.append(self)

s1 = Sample(10, name="Bob")
print s1
print s1.args
print s1.kwds

s2 = Sample("red", name="Bill")
print s2
print s2.args
print s2.kwds

s1.release()
s3 = Sample("blue", name="Tim")
print s3
print s3.args
print s3.kwds
Oops. This line:
temp = object.__new__(Sample, args, kwds)
should be:

temp = object.__new__(cls, args, kwds)

although it would seem that cls is always going to be Sample, so I'm
not sure what practical difference that makes.

Jun 3 '07 #5
On Sat, 02 Jun 2007 23:25:49 -0700, 7stud wrote:
Oops. This line:
>temp = object.__new__(Sample, args, kwds)

should be:

temp = object.__new__(cls, args, kwds)

although it would seem that cls is always going to be Sample, so I'm
not sure what practical difference that makes.
What if you are calling it from a sub-class?
--
Steven.

Jun 3 '07 #6
On Jun 3, 12:50 am, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
On Sat, 02 Jun 2007 23:25:49 -0700, 7stud wrote:
Oops. This line:
temp = object.__new__(Sample, args, kwds)
should be:
temp = object.__new__(cls, args, kwds)
although it would seem that cls is always going to be Sample, so I'm
not sure what practical difference that makes.

What if you are calling it from a sub-class?

--
Steven.
cls it is!

Jun 3 '07 #7
On Jun 3, 2:17 pm, 7stud <bbxx789_0...@yahoo.comwrote:
On Jun 2, 10:31 pm, lialie <lia...@gmail.comwrote:
Hi,
suppose i have a free_object list[Sample1, Smaple2....]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.
any way to do this?
I do some work as follows:
class Sample(object):
used_object = []
free_object = []
def __init__(self, *args, **kwds):
pass
def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj
######## still get a new instance :(
def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True

This seems to work for me:

import collections

class Sample(object):

free_objects = collections.deque()
used_objects = []

def __new__(cls, *args, **kwds):
if not Sample.free_objects:
temp = object.__new__(Sample, args, kwds)
Sample.used_objects.append(temp)
return temp
else:
return Sample.free_objects.popleft()

def __init__(self, *args, **kwds):
self.args = args
self.kwds = kwds

def release(self):
Sample.used_objects.remove(self)
Sample.free_objects.append(self)

s1 = Sample(10, name="Bob")
print s1
print s1.args
print s1.kwds

s2 = Sample("red", name="Bill")
print s2
print s2.args
print s2.kwds

s1.release()
s3 = Sample("blue", name="Tim")
print s3
print s3.args
print s3.kwds
Thank you.
def __new__(cls, *args, **kwds):
if not Sample.free_objects:
temp = object.__new__(Sample, args, kwds)
Sample.used_objects.append(temp)
return temp
else:
return Sample.free_objects.popleft() # Here you may lost one object :)
The free_objects list doesn't have orders. My sample code seems to
work. But follows may create a new instance:

class Sample(wx.Image):
free_objects = []
used_objects
def __int__(self, *args, **kwds):
wx.Image.__init__(self, *args, **kwds) #### play tricks with
me :)
pass

def __new__(cls, *args, **kwds):
# The same

wx.Image always create a new instance. It seems that I have to avoid
wx.Image.__init__.

Jun 3 '07 #8

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

Similar topics

3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
1
by: I am Sam | last post by:
using System; using System.Drawing; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.ComponentModel;...
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
0
by: AMDRIT | last post by:
Hello everyone, VB2003-Winforms I have a class object (testclass), that I would like to bind parts of to two custom controls (control1 and control2). The testclass is stored in a hash table...
4
by: tony | last post by:
Hello! If I want to prevent creation of object for a class. I can accomplish this by using different technique. One way of doing this is to define the class to be abstract even if all the...
2
by: --== Alain ==-- | last post by:
Hi, I have some issue to debug my custom control. For example, in my solution i have 2 projects. 1 project is my custom control and the other one, is a windows form application. I would...
7
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the...
1
by: mitrofun63 | last post by:
Hi, All I have trouble with db2 instance creation (DB2 9.1.2 on AIX 5.3) When i run command for instance creation : ../db2icrt -a SERVER -p 50000 -s ese -u db2fenc1 db2inst1 a reciveve...
31
by: Tom P. | last post by:
I am doing quite a bit of custom painting and it means I have to create a lot of brushes (think one for every file system object in a directory) per paint. How expensive is this? Should I find a...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.