By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

How do I create lists in classes in Python?

P: 3
I want to operate a local list in a class in Python. I can not do that:
Expand|Select|Wrap|Line Numbers
  1. class cCompetition:
  2.     lcPersonAll = []
  3.  
  4.     def AddPerson(self, cPersonNew):
  5.         lcPersonAll.append(cPersonNew)
  6.  
  7. cCompetition1 = cCompetition()
  8. cCompetition1.AddPerson("Smith")
However, global list is possible.
Jan 9 '17 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 613
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)
Jan 9 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.