How do I create lists in classes in Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kosarar
    New Member
    • Dec 2014
    • 3

    How do I create lists in classes in Python?

    I want to operate a local list in a class in Python. I can not do that:
    Code:
    class cCompetition:
        lcPersonAll = []
     
        def AddPerson(self, cPersonNew):
            lcPersonAll.append(cPersonNew)
     
    cCompetition1 = cCompetition()
    cCompetition1.AddPerson("Smith")
    However, global list is possible.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    It has to be an instance object of the class (self). Since this is in most tutorials, start with one that you like https://wiki.python.org/moin/BeginnersGuide/Programmers For an example of using a list in a class, see "Objects and Stuff" at http://hetland.org/writing/instant-python.html You have a class, not an instance, object --> difference http://www.tutorialspoint.com/python...es_objects.htm (empCount vs self.name & self.salary)

    Comment

    Working...