making objects with individual attributes!

Diez B. Roggisch deets at nospam.web.de
Tue Mar 20 17:27:21 CET 2007


Alejandro 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"

Yes, by using instance-attributes instead of class-attributes, as you do.

Btw, it's common to name classes in python with a capital first letter, and
you should use new-style classes, which means you need to subclass it from
object:

class Document(object):

    def __init__(self):
        self.haveWords = set()


Diez



More information about the Python-list mailing list