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

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 1226
bytecolor <by*******@yahoo.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.drawables.append(weakref.ref(self))

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

--
bytecolor

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

In each __init__() I use:
aptbase.drawables.append(weakref.ref(self))

Then in show():
for d in aptbase.drawables:
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.drawables = weakref.WeakValueDictionary

then in each __init__

aptbase.drawables[len(aptbase.drawables)] = self

then in show:

for o in aptbase.drawables.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 WeakValueDictionary'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.drawables = weakref.WeakValueDictionary typo here:
aptbase.drawables = weakref.WeakValueDictionary() then in each __init__
aptbase.drawables[len(aptbase.drawables)] = self
then in show:
for o in aptbase.drawables.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.drawables[id(self)] = self

--
-Scott David Daniels
sc***********@acm.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.drawables = weakref.WeakValueDictionary

typo here:
aptbase.drawables = weakref.WeakValueDictionary()
then in each __init__
aptbase.drawables[len(aptbase.drawables)] = self
then in show:
for o in aptbase.drawables.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.drawables[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.drawables = weakref.WeakValueDictionary()
then in each __init__
aptbase.drawables[len(aptbase.drawables)] = self
then in show:
for o in aptbase.drawables.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***********@acm.org
Feb 1 '06 #7

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

Similar topics

4
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
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...
6
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...
6
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? ...
2
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...
3
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...
6
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...
16
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...
2
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. ...
28
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.