473,800 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

instance references?

I'm working on a simple graphics package. I've got a function show()
that the user needs to call at the end of the script to actually
display the points, lines and circles that have been defined in the
script.

p1 = point(0, 0)
l1 = line(1, 3, -4, 5)
c1 = circle(-2, 3, 1)
show()

In each __init__() method of the point, line, and circle objects I add
self to a list in a separate module, then show() uses this list to
actually draw the objects. This works except:

l1 = line(point(1, 3), point(-4, 5))
c1 = circle(point(-2, 3), 1)

adds the references to the points used to define the objects to the
render list. I only want objects that have been assigned to a variable
to be rendered. (Try to ignore the function overloading.)

So how can I tell if an instance of point, line or circle has actually
been assigned to a variable?

--
bytecolor

Jan 29 '06 #1
6 1239
bytecolor <by*******@yaho o.com> wrote:
...
So how can I tell if an instance of point, line or circle has actually
been assigned to a variable?


You can't really distinguish whether an object has been assigned to a
variable, an item in a list, an attribute, etc etc -- all of these
assignments are much the same thing, there is nothing special about
variables to distinguish them from the other kind of 'labels'. Indeed,
for example, global variables are EXACTLY the same thing as attributes
of a module objects which in turn are exactly the same things as values
in the module's __dict__ -- so, any hopes of distinguishing between
variables, attributes, and items inside a container, must clearly be
abandoned.

You can do various other things that might come close to what you want,
such as using weak references in your list (if no 'true' reference
exists, an object goes away and _weak_ references to that object let you
check if the object is still around or has gone away) -- that will let
you know whether the object has been assigned to *anything* (you can
perform a similar task by checking on the number of references extant to
an object). But checking if the existing references are indeed
'variables' or something else is a hard, unrewarding task.
Alex

Jan 30 '06 #2
Thanks Alex, the weak references *seem* to be doing what I want for
now.

In each __init__() I use:
aptbase.drawabl es.append(weakr ef.ref(self))

Then in show():
for d in aptbase.drawabl es:
o = d()
if o is not None:
# render it

--
bytecolor

Jan 30 '06 #3
bytecolor <by*******@yaho o.com> wrote:
Thanks Alex, the weak references *seem* to be doing what I want for
now.

In each __init__() I use:
aptbase.drawabl es.append(weakr ef.ref(self))

Then in show():
for d in aptbase.drawabl es:
o = d()
if o is not None:
# render it


Looks good, so apparently you didn't particularly care about assignment
to variables vs assignment to other "labels" -- great!

My favourite way to use weakref is slightly different: I would have

aptbase.drawabl es = weakref.WeakVal ueDictionary

then in each __init__

aptbase.drawabl es[len(aptbase.dra wables)] = self

then in show:

for o in aptbase.drawabl es.values():
# render it
Not a vastly different idiom from yours, mind you, but I like not having
to check whether an object has gone away -- a WeakValueDictio nary's
entries just disappear when the value object goes away, so that at any
time looping on its .values() is safe as brick houses!-)
Alex
Jan 30 '06 #4
Alex Martelli wrote:
My favourite way to use weakref is slightly different: I would have
aptbase.drawabl es = weakref.WeakVal ueDictionary typo here:
aptbase.drawabl es = weakref.WeakVal ueDictionary() then in each __init__
aptbase.drawabl es[len(aptbase.dra wables)] = self
then in show:
for o in aptbase.drawabl es.values():
# render it

The keys you are choosing are probably not a good idea.
Problem demo:

a,b,c = Something(), Something(), Something()
b = None
d = Something() # overwrites the d entry.

I'd use:
aptbase.drawabl es[id(self)] = self

--
-Scott David Daniels
sc***********@a cm.org
Jan 30 '06 #5
Scott David Daniels <sc***********@ acm.org> wrote:
Alex Martelli wrote:
My favourite way to use weakref is slightly different: I would have
aptbase.drawabl es = weakref.WeakVal ueDictionary

typo here:
aptbase.drawabl es = weakref.WeakVal ueDictionary()
then in each __init__
aptbase.drawabl es[len(aptbase.dra wables)] = self
then in show:
for o in aptbase.drawabl es.values():
# render it

The keys you are choosing are probably not a good idea.
Problem demo:

a,b,c = Something(), Something(), Something()
b = None
d = Something() # overwrites the d entry.

I'd use:
aptbase.drawabl es[id(self)] = self


Good point -- the id isn't going to be reused until self is gone, of
course. I normally use a monotonically increasing counter (global or in
self's class), but there's no real reason for that -- id is simpler (and
using len as I had is definitely bugprone, as you mention).
Alex
Jan 31 '06 #6
Scott David Daniels wrote:
Alex Martelli wrote: (in effect)
aptbase.drawabl es = weakref.WeakVal ueDictionary()
then in each __init__
aptbase.drawabl es[len(aptbase.dra wables)] = self
then in show:
for o in aptbase.drawabl es.values():
# render it


The keys you are choosing are probably not a good idea.
Problem demo:
a,b,c = Something(), Something(), Something()
b = None
d = Something() # overwrites the d entry.


In a tiny demo of what exactly the problem is, I managed to
mess up the comment.
a, b, c = Something(), Something(), Something()
b = None
d = Something() # overwrites the _c_ entry.

Silly me.

--Scott David Daniels
sc***********@a cm.org
Feb 1 '06 #7

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

Similar topics

4
3917
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
4
1908
by: Donnal Walter | last post by:
This is a question about Python patterns or idioms. Over a period of time, I have evolved a pattern of usage that seems to work better for me than other ways I tried previously, but in writing some documentation I don't know what to call this syntax or how best to describe it. I have not seen it used in other places. The somewhat longish version is that I have implemented an MVP (model-view-presenter) architecture where each "presenter"...
6
22539
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
6
8929
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? I am running UDB 8.2 on Linux and AIX. Thanks.
2
1124
by: Hutty | last post by:
The following line is creating the above error message. tree_nodes((level)-1).Nodes.Add(tree_nodes(level)) It's because of the following line where I'm trying to calculate the length of blank spaces and substracting from total length. level = text_line.Length - text_line.TrimStart.Length Here's the code. The first part of the "IF" is fine.
3
4237
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
6
2824
by: Jason Bell | last post by:
How can I find out how many references to an instance exist at any given time? I want to make it so that when the number of references is one, a countdown starts (that one reference being the one held by the resource manager). This way I can ensure that objects are only deleted if they haven't been used in an amount of time defined by the user.
16
2706
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
2
2010
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. class AnyObject: pass class Container: links = def add(self,other):
28
2189
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is that it simplifies the simulator very much. In this translation I want to keep the JAL-syntax as much as possible intact, so anyone who can read JAL, can also understand the Python syntax. One of the problems is the alias statement, assigning a...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10273
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
3
2944
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.