Possible to tack on random stuff to objects?

2008-02-11 Thread Cruxic
Is it possible to tack on arbitrary attributes to a python object?
For example:

s = 'nice 2 meet you'
s.isFriendly = True

In the above example Python complains on the second line with:

AttributeError: 'str' object has no attribute 'isFriendly'

Is there another way?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Possible to tack on random stuff to objects?

2008-02-12 Thread Cruxic
That does the trick.  Thanks, Bruno.

On Feb 12, 1:23 am, Bruno Desthuilliers  wrote:
> Cruxic a écrit :
>
> > Is it possible to tack on arbitrary attributes to a python object?
>
> Depends on the object's class. In the common case it's possible but
> there are a couple cases - mostly builtin immutable types - where it's not.
>
> > For example:
>
> > s = 'nice 2 meet you'
> > s.isFriendly = True
>
> > In the above example Python complains on the second line with:
>
> > AttributeError: 'str' object has no attribute 'isFriendly'
>
> > Is there another way?
>
>  >>> class MyString(str): pass
> ...
>  >>> s = MyString("hello")
>  >>> s.is_friendly = True
>  >>> s
> 'hello'
>  >>>

-- 
http://mail.python.org/mailman/listinfo/python-list


Can't get items out of a set?

2008-03-07 Thread Cruxic
Hello, all.

Is it possible to get an object out of a set() given another object
that has the same hash code and equality (__hash__() and __eq__()
return the same)?

You can't do this with Java Sets either and I've needed it on multiple
occasions.  Doesn't it seem like it would be useful?  Consider:

class Person:
  def __init__(self, id, name):
self.id = id
self.name = name

  def __hash__(self):
return self.id

  def __eq__(self, other):
return self.id == other.id


people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
...
p = people.get_equivalent(2)  #method doesn't exist as far as I know
print p.name  #prints Sue


I'm not sure if the above code compiles but I hope you get the idea.
Is it possible?
Much Thanks.
- Cruxic
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't get items out of a set?

2008-03-08 Thread Cruxic
On Mar 7, 11:20 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
> [Cruxic]
>
> > Is it possible to get an object out of a set() given another object
> > that has the same hash code and equality (__hash__() and __eq__()
> > return the same)?
>
> Yes, but it requires an indirect 
> approach.http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/499299
>
> Raymond

That's a clever work around.  Thanks, Raymond.  Clearly you had a need
for this.  Do you feel it's a common need that should be submitted as
a Python feature request?  To me it seems like such a simple thing
that would increase the general utility of the set class.  I suppose I
could start another thread like "feature request: New method for set -
get_equivalent".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't get items out of a set?

2008-03-08 Thread Cruxic
On Mar 8, 7:32 am, Alan Isaac <[EMAIL PROTECTED]> wrote:
> Cruxic wrote:
> > people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
> > ...
> > p = people.get_equivalent(2)  #method doesn't exist as far as I know
> > print p.name  #prints Sue
>
> def get_equivalent(test, container):
>
>   for p in container:
>
> if p == test:
>
>   return p
>
> hth,
>
> Alan Isaac
>
> #example (note change in __eq__ to match your case; fix if nec)
>
> class Person:
>
>   def __init__(self, id, name):
>
> self.id = id
>
> self.name = name
>
>   def __hash__(self):
>
> return self.id
>
>   def __eq__(self, other):
>
> return self.id == other
>
> people = set( [Person(1, 'Joe'), Person(2, 'Sue')] )
>
> get_equivalent(2,people)

That works fine for small data sets but my goal is to avoid a linear
search, instead leveraging the O(1) lookup time for a hash based set.
-- 
http://mail.python.org/mailman/listinfo/python-list