Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-15 Thread Guilherme P. de Freitas
Thanks everybody for all the suggestions and ideas. I still did not
have time to look them all over, but I will. Thanks again!

Best,

Guilherme

On Thu, Jan 14, 2010 at 4:52 AM, Alan Gauld alan.ga...@btinternet.com wrote:

 Guilherme P. de Freitas guilhe...@gpfreitas.com wrote

 Here is my problem. I have two classes, 'Body' and 'Member', and some
 attributes of 'Body' can be of type 'Member', but some may not. The
 precise attributes that 'Body' has depend from instance to instance,
 and they can be added or deleted. I need any instance of 'Body' to
 keep an up-to-date list of all its attributes that belong to the class
 'Member'. How do I do this?

 Check the type of the attribute? Using isinstance()?

 I'm not sure of the rationale - why it needs to know the type etc
 But isinstance is the most obvious way that I can think of.

 Alan G.

 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
Guilherme P. de Freitas
http://www.gpfreitas.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-15 Thread Guilherme P. de Freitas
Ok, I checked the answers you all gave me, and the suggestions seem to be 2:

a. Compute the list of members on the fly, with a list comprehension
that looks up stuff on self.__dict___;

b. Stay with my solution, but substituting the unnecessary if-else
by a simple if. In sum, maintain a list of members that is updated
whenever a member is added or removed.

Which one is better? I am new to all this, so please correct me if I
am wrong. It seems that (a) is better if you do a lot of
adding/removing members operations compared to the number of lookups
to the members list. Otherwise, (b) is better. Right?

In my application, I will do many more lookups on the members list
than adding/removing members (this sounds weird, feels like I'm Dr.
Frankenstein), so I will go with (b).

Thanks again for all the suggestions,

Guilherme

-- 
Guilherme P. de Freitas
http://www.gpfreitas.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-15 Thread Kent Johnson
On Fri, Jan 15, 2010 at 3:43 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Ok, I checked the answers you all gave me, and the suggestions seem to be 2:

 a. Compute the list of members on the fly, with a list comprehension
 that looks up stuff on self.__dict___;

 b. Stay with my solution, but substituting the unnecessary if-else
 by a simple if. In sum, maintain a list of members that is updated
 whenever a member is added or removed.

 Which one is better? I am new to all this, so please correct me if I
 am wrong. It seems that (a) is better if you do a lot of
 adding/removing members operations compared to the number of lookups
 to the members list. Otherwise, (b) is better. Right?

Personally (b) feels more direct and robust. There is no guarantee
that attributes are stored in self.__dict__, they can be in __slots__
or delegated to some other container. For example, here is a class
with no __dict__:

In [1]: class Foo(object):
   ...: __slots__ = ['bar']
   ...: def __setattr__(self, name, value):
   ...: print 'set', name, 'to', value
   ...: object.__setattr__(self, name, value)

In [2]: f=Foo()

In [3]: f.bar = 'baz'
set bar to baz

In [4]: f.__dict__
---
AttributeErrorTraceback (most recent call last)

C:\Project\(misc)\MangoDB\ipython console in module()

AttributeError: 'Foo' object has no attribute '__dict__'

Granted if you have control over all the classes this won't happen but
still...relying on __dict__ makes me nervous.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Dave Angel
(You top-posted, which puts your two comments out of order.  Now the 
solution comes before the problem statement)


Guilherme P. de Freitas wrote:

Ok, I got something that seems to work for me. Any comments are welcome.


class Member(object):
def __init__(self):
pass


class Body(object):
def __init__(self):
self.members = []

def __setattr__(self, obj, value):
if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)
else:
object.__setattr__(self, obj, value)

def __delattr__(self, obj):
if isinstance(getattr(self, obj), Member):
self.members.remove(obj)
object.__delattr__(self, obj)
else:
object.__delattr__(self, obj)



john = Body()
john.arm = Member()
print(john.members)
del john.arm
print(john.members)


On Wed, Jan 13, 2010 at 6:24 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
  

Hi everybody,

Here is my problem. I have two classes, 'Body' and 'Member', and some
attributes of 'Body' can be of type 'Member', but some may not. The
precise attributes that 'Body' has depend from instance to instance,
and they can be added or deleted. I need any instance of 'Body' to
keep an up-to-date list of all its attributes that belong to the class
'Member'. How do I do this?

Best,

Guilherme

--
Guilherme P. de Freitas
http://www.gpfreitas.com


If this is a class assignment, you've probably got the desired answer.  
But if it's a real-world problem, there are tradeoffs, and until those 
are known, the simplest solution is usually the best.  Usually, what's 
desired is that the object behaves as if it has an up-to-date list of...


If order of the list doesn't matter, I'd consider simply writing a 
single method, called 'members' which does a list comprehension of the 
object's attributes, calculating the list when needed.  Then use a 
decorator to make this method look like a read-only data attribute  
(untested):


class Body (object):
@property
 def members(self):
return  [obj for .   if  ]

DaveA



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 9:24 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Hi everybody,

 Here is my problem. I have two classes, 'Body' and 'Member', and some
 attributes of 'Body' can be of type 'Member', but some may not. The
 precise attributes that 'Body' has depend from instance to instance,
 and they can be added or deleted. I need any instance of 'Body' to
 keep an up-to-date list of all its attributes that belong to the class
 'Member'. How do I do this?

If you want to keep track of attributes as they are added and deleted,
you should override __setattr__() and __delattr__() in your Body
class.
http://docs.python.org/reference/datamodel.html#customizing-attribute-access
Here is a simple example:

In [4]: class LogAttributes(object):
   ...: def __setattr__(self, name, value):
   ...: print 'setting', name, 'to', value, type(value)
   ...: object.__setattr__(self, name, value)
   ...: def __delattr__(self, name):
   ...: print 'removing', name
   ...: object.__delattr__(self, name)

In [5]: l = LogAttributes()

In [6]: l.foo = 'bar'
setting foo to bar type 'str'

In [7]: l.foo
Out[7]: 'bar'

In [8]: del l.foo
removing foo

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 11:15 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Ok, I got something that seems to work for me. Any comments are welcome.


 class Member(object):
    def __init__(self):
        pass


 class Body(object):
    def __init__(self):
        self.members = []

    def __setattr__(self, obj, value):
        if isinstance(value, Member):
            self.members.append(obj)
            object.__setattr__(self, obj, value)
        else:
            object.__setattr__(self, obj, value)

That's fine but there is no need to duplicate the object.__setattr__() call:
   def __setattr__(self, obj, value):
   if isinstance(value, Member):
   self.members.append(obj)
   object.__setattr__(self, obj, value)

    def __delattr__(self, obj):
        if isinstance(getattr(self, obj), Member):
            self.members.remove(obj)
            object.__delattr__(self, obj)
        else:
            object.__delattr__(self, obj)

Same here.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread spir
On Wed, 13 Jan 2010 20:15:21 -0800
Guilherme P. de Freitas guilhe...@gpfreitas.com wrote:

 Ok, I got something that seems to work for me. Any comments are welcome.
 
 
 class Member(object):
 def __init__(self):
 pass
 
 
 class Body(object):
 def __init__(self):
 self.members = []
 
 def __setattr__(self, obj, value):
 if isinstance(value, Member):
 self.members.append(obj)
 object.__setattr__(self, obj, value)
 else:
 object.__setattr__(self, obj, value)
 
 def __delattr__(self, obj):
 if isinstance(getattr(self, obj), Member):
 self.members.remove(obj)
 object.__delattr__(self, obj)
 else:
 object.__delattr__(self, obj)

Seems perfectly ok to me, except for if...else constructs in place of simple 
if's: an optional statement is added in case obj is of type Member; this is not 
an alternative.

if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)# in both cases

Same for __delattr__, indeed.

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Alan Gauld


Guilherme P. de Freitas guilhe...@gpfreitas.com wrote


Here is my problem. I have two classes, 'Body' and 'Member', and some
attributes of 'Body' can be of type 'Member', but some may not. The
precise attributes that 'Body' has depend from instance to instance,
and they can be added or deleted. I need any instance of 'Body' to
keep an up-to-date list of all its attributes that belong to the class
'Member'. How do I do this?


Check the type of the attribute? Using isinstance()?

I'm not sure of the rationale - why it needs to know the type etc
But isinstance is the most obvious way that I can think of.

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Keeping a list of attributes of a certain type

2010-01-13 Thread Guilherme P. de Freitas
Hi everybody,

Here is my problem. I have two classes, 'Body' and 'Member', and some
attributes of 'Body' can be of type 'Member', but some may not. The
precise attributes that 'Body' has depend from instance to instance,
and they can be added or deleted. I need any instance of 'Body' to
keep an up-to-date list of all its attributes that belong to the class
'Member'. How do I do this?

Best,

Guilherme

-- 
Guilherme P. de Freitas
http://www.gpfreitas.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-13 Thread Guilherme P. de Freitas
Ok, I got something that seems to work for me. Any comments are welcome.


class Member(object):
def __init__(self):
pass


class Body(object):
def __init__(self):
self.members = []

def __setattr__(self, obj, value):
if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)
else:
object.__setattr__(self, obj, value)

def __delattr__(self, obj):
if isinstance(getattr(self, obj), Member):
self.members.remove(obj)
object.__delattr__(self, obj)
else:
object.__delattr__(self, obj)



john = Body()
john.arm = Member()
print(john.members)
del john.arm
print(john.members)


On Wed, Jan 13, 2010 at 6:24 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Hi everybody,

 Here is my problem. I have two classes, 'Body' and 'Member', and some
 attributes of 'Body' can be of type 'Member', but some may not. The
 precise attributes that 'Body' has depend from instance to instance,
 and they can be added or deleted. I need any instance of 'Body' to
 keep an up-to-date list of all its attributes that belong to the class
 'Member'. How do I do this?

 Best,

 Guilherme

 --
 Guilherme P. de Freitas
 http://www.gpfreitas.com




-- 
Guilherme P. de Freitas
http://www.gpfreitas.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor