[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

RE: Newbie question on Classes

Reedick, Andrew

1/10/2008 10:27:00 PM

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Adrian Wood
> Sent: Thursday, January 10, 2008 4:47 PM
> To: python-list@python.org
> Subject: Newbie question on Classes
>
>
> I can call man.state() and then woman.state() or Person.state(man) and
> Person.state(woman) to print the status of each. This takes time and
> space however, and becomes unmanageable if we start talking about a
> large number of objects, and unworkable if there is an unknown number.
> What I'm after is a way to call the status of every instance of Man,
> without knowing their exact names or number.
>



How about searching the garbage collector?

import gc
from random import random

class Person:
def __init__(self):
self.data= random() * 1000 + 1

def WhoAmI(self):
return self.data


a = Person()
b = Person()

for i in gc.get_objects():
try:
# classes have __class__ and __dict__ defined according to the
docs
if i.__class__ and i.__dict__ :
print i.__class__
if str(i.__class__) == '__main__.Person':
print "\tI am", i.WhoAmI()
except:
pass



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622


2 Answers

John Machin

1/11/2008 9:08:00 PM

0

On Jan 11, 9:27 am, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=att....@python.org [mailto:python-
> > list-bounces+jr9445=att....@python.org] On Behalf Of Adrian Wood
> > Sent: Thursday, January 10, 2008 4:47 PM
> > To: python-l...@python.org
> > Subject: Newbie question on Classes
>
> > I can call man.state() and then woman.state() or Person.state(man) and
> > Person.state(woman) to print the status of each. This takes time and
> > space however, and becomes unmanageable if we start talking about a
> > large number of objects, and unworkable if there is an unknown number.
> > What I'm after is a way to call the status of every instance of Man,
> > without knowing their exact names or number.
>
> How about searching the garbage collector?

Not a good idea, because gc is an implementation artifact and in fact
is specific to the C Python implementation; you won't find it in e.g.
Iron Python or Jython.

Not a good idea, because you are replacing iterating over a collection
of all the objects of interest (and *only* those of interest) with a
clumsy [see below] rummage through a potentially far much larger
collection of objects.

>
> import gc
> from random import random
>
> class Person:
> def __init__(self):
> self.data= random() * 1000 + 1
>
> def WhoAmI(self):
> return self.data
>
> a = Person()
> b = Person()
>
> for i in gc.get_objects():
> try:
> # classes have __class__ and __dict__ defined according to the
> docs

Again, implementation artifacts.


> if i.__class__ and i.__dict__ :

In general, i.__dict_ could be empty. You probably mean
hasattr('__dict__')

> print i.__class__
> if str(i.__class__) == '__main__.Person':

and what if the class is not defined in __main__ but in some other
module?

Have you considered
i.__class__ is Person
or the usual (and underscore-free) idiom
isinstance(i, Person) ?

> print "\tI am", i.WhoAmI()
> except:
> pass

Blindly ignoring all possible exceptions is A Very Bad Idea.

Reedick, Andrew

1/11/2008 9:29:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of John Machin
> Sent: Friday, January 11, 2008 4:08 PM
> To: python-list@python.org
> Subject: Re: Newbie question on Classes
>
> On Jan 11, 9:27 am, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
> > > -----Original Message-----
> > > From: python-list-bounces+jr9445=att....@python.org
[mailto:python-
> > > list-bounces+jr9445=att....@python.org] On Behalf Of Adrian Wood
> > > Sent: Thursday, January 10, 2008 4:47 PM
> > > To: python-l...@python.org
> > > Subject: Newbie question on Classes
> >
> >
> > How about searching the garbage collector?
>
> Not a good idea, because gc is an implementation artifact and in fact
> is specific to the C Python implementation; you won't find it in e.g.
> Iron Python or Jython.
>

a) I forgot the ';-)' and/or <tongue_in_cheek> tag.

b) It was junk code. A cleaner loop would be (which I figured out 5
minutes after posting =P ):
for i in gc.get_objects():
if isinstance(i, Person):
print i.__class__, "%x" % id(i), i.SomeMethod()

c) It's good to know about GC being CPython only.

d) You forgot to mention the problem that some of the objects could be
'deleted' but not garbage collected yet.

e) Most importantly, anyone who is using the garbage collector as their
object manager isn't into proper coding practices in the first place.