defining class and list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    defining class and list

    I want to create a list of integers and user will have the right to add into this list a number of elements that user wants. How can I do that?

    [code=python]
    class createList:
    def __ini__(self):
    self._listInt = list()

    def __addValue(self , newValue):
    self._ListInt.a ppend(newvalue)


    >>>AttributeErr or: createList intance has no attribute '_listInt'
    [/code]
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by python101
    I want to create a list of integers and user will have the right to add into this list a number of elements that user wants. How can I do that?

    [code=python]
    class createList:
    def __ini__(self):
    self._listInt = list()

    def __addValue(self , newValue):
    self._ListInt.a ppend(newvalue)


    >>>AttributeErr or: createList intance has no attribute '_listInt'
    [/code]
    You spelled your method wrong. It shuold be:
    [code=python]
    def __init__(self):
    [/code]

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      It was a mistake of posting, the error message still remained

      I found out that the some indicators in in lines ^_^

      If I want to define a function to check whether an integer in list (return True or False), what should I do?
      [code=python]
      a=createList()
      a=addValue(2)

      >> 2 in a
      >>error[/code]

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by python101
        It was a mistake of posting, the error message still remained

        I found out that the some indicators in in lines ^_^

        If I want to define a function to check whether an integer in list (return True or False), what should I do?
        [code=python]
        a=createList()
        a=addValue(2)

        >> 2 in a
        >>error[/code]
        You overload the in operator:
        [code=python]
        ...
        def __contains__(se lf, obj):
        # do whatever you want and return True or False
        [/code]

        Comment

        • python101
          New Member
          • Sep 2007
          • 90

          #5
          Originally posted by ilikepython
          You overload the in operator:
          [code=python]
          ...
          def __contains__(se lf, obj):
          # do whatever you want and return True or False
          [/code]
          First time see this, thank you very much ^_^

          anyway, do we have a way to add n integers into the list? If I use

          [code=python]
          def __addValue(self , newValue):

          # I can only add 1, however I want to do ListA.addvalue( 2,3,4,5...)

          [/code]

          Comment

          • elcron
            New Member
            • Sep 2007
            • 43

            #6
            Originally posted by python101
            I want to create a list of integers and user will have the right to add into this list a number of elements that user wants. How can I do that?

            [code=python]
            class createList:
            def __ini__(self):
            self._listInt = list()

            def __addValue(self , newValue):
            self._ListInt.a ppend(newvalue)


            >>>AttributeErr or: createList intance has no attribute '_listInt'
            [/code]
            Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
            [code=python]
            class createList:
            def __init__(self):
            self._listInt = list()

            def __addValue(self , newValue):
            self._listInt.a ppend(newvalue)

            def contains(self, item):
            return item in self.listInt

            test = createList()
            test.contains(s omeItem)
            [/code]
            Or operator overloading
            [code=python]
            class createList:
            def __init__(self):
            self._listInt = list()

            def __addValue(self , newValue):
            self._listInt.a ppend(newvalue)

            def __contains__(se lf, item):
            return item in self.listInt

            test = createList()
            someItem in test
            [/code]
            Here's a good link on emulating data types and you could always try subclassing a list.

            Edit: Sorry for posting what was already stated I had to do something before I could finish posting.

            Comment

            • python101
              New Member
              • Sep 2007
              • 90

              #7
              Originally posted by elcron
              Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
              [code=python]
              class createList:
              def __init__(self):
              self._listInt = list()

              def __addValue(self , newValue):
              self._listInt.a ppend(newvalue)

              def contains(self, item):
              return item in self.listInt

              test = createList()
              test.contains(s omeItem)
              [/code]
              Or operator overloading
              [code=python]
              class createList:
              def __init__(self):
              self._listInt = list()

              def __addValue(self , newValue):
              self._listInt.a ppend(newvalue)

              def __contains__(se lf, item):
              return item in self.listInt

              test = createList()
              someItem in test
              [/code]
              Here's a good link on emulating data types and you could always try subclassing a list.

              Edit: Sorry for posting what was already stated I had to do something before I could finish posting.
              thank you very much for your help, I had finished this

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by elcron
                Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
                [code=python]
                class createList:
                def __init__(self):
                self._listInt = list()

                def __addValue(self , newValue):
                self._listInt.a ppend(newvalue)

                def contains(self, item):
                return item in self.listInt

                test = createList()
                test.contains(s omeItem)
                [/code]
                Or operator overloading
                [code=python]
                class createList:
                def __init__(self):
                self._listInt = list()

                def __addValue(self , newValue):
                self._listInt.a ppend(newvalue)

                def __contains__(se lf, item):
                return item in self.listInt

                test = createList()
                someItem in test
                [/code]
                Here's a good link on emulating data types and you could always try subclassing a list.

                Edit: Sorry for posting what was already stated I had to do something before I could finish posting.
                I made some minor modifications to the code you posted. The use of double preceeding underscores "privatizes " a variable - Python mangles the name of the method or attribute. A function or method will accept a variable number of parameters if an asterisk (*) is added to the last parameter name.[code=Python]class createList:
                def __init__(self, *args):
                self._listInt = list(args)

                def __addValue(self , *newValue):
                self._listInt.e xtend(newValue)

                def contains(self, item):
                return item in self._listInt

                test = createList(9,2, 3,6,8)
                print test._listInt
                test._createLis t__addValue(21, 22,23)
                print test._listInt
                print test.contains(9 )


                class createList:
                def __init__(self, *args):
                self._listInt = list(args)

                def __addValue(self , *newValue):
                self._listInt.e xtend(newValue)

                def __contains__(se lf, item):
                return item in self._listInt

                test = createList(22,2 3,24)
                print test._listInt
                test._createLis t__addValue(3,4 ,5,6)
                print test._listInt
                test._createLis t__addValue(36)
                print test._listInt
                print 36 in test[/code]
                Output:

                >>> [9, 2, 3, 6, 8]
                [9, 2, 3, 6, 8, 21, 22, 23]
                True
                [22, 23, 24]
                [22, 23, 24, 3, 4, 5, 6]
                [22, 23, 24, 3, 4, 5, 6, 36]
                True
                >>>

                Comment

                Working...