making objects with individual attributes!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alejandro

    making objects with individual attributes!

    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"

  • kyosohma@gmail.com

    #2
    Re: making objects with individual attributes!

    On Mar 20, 11:08 am, "Alejandro" <altud...@yahoo .comwrote:
    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

    Comment

    • Gary Herron

      #3
      Re: making objects with individual attributes!

      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"
      >
      Of course. Try this:

      class document:
      def __init__(self, string):
      self.titre = string
      self.haveWords = set()

      Each instance creation will call __init__ with the instance
      accessible through self, and that code will create two instance
      specific attributes.

      Don't use so called class level attributes (as in your example)
      unless you *want* sharing between all instances of a class.

      Gary Herron




      Comment

      • Diez B. Roggisch

        #4
        Re: making objects with individual attributes!

        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

        Comment

        • Alejandro

          #5
          Re: making objects with individual attributes!

          Thanks to all!!! Now it works!

          Comment

          Working...