making objects with individual attributes!

kyosohma at gmail.com kyosohma at gmail.com
Tue Mar 20 12:25:19 EDT 2007


On Mar 20, 11:08 am, "Alejandro" <altud... at yahoo.com> wrote:
> I have created a class:
>
> class document:
>
>     titre = ''
>     haveWords = set()
>
>     def __init__(self, string):
>
>         self.titre = string
>
> #########
>
> doc1 = document('doc1')
> doc2 = document('doc2')
>
> doc1.haveWords.add(1)
> doc2.haveWords.add(2)
>
> print doc1.haveWords
>
> # i get set([1, 2])
>
> doc1 and doc are sharing attribute haveWords!
> Why ??? there's a way to assign every objetc "document" a different
> "set"

You're just slightly off. The code needs to be more like this:

class document:

	    def __init__(self, string):

	        self.titre = string
	        self.haveWords = set()

##################

I think what you did was create some kind of global variable that was
then accessible through the namespace. But I really don't know for
sure. This code seems to work now, though.

Mike




More information about the Python-list mailing list