473,288 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __ini__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._ListInt.append(newvalue)
  7.  
  8.  
  9. >>>AttributeError: createList intance has no attribute '_listInt'
  10.  
Oct 15 '07 #1
7 1510
ilikepython
844 Expert 512MB
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?

Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __ini__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._ListInt.append(newvalue)
  7.  
  8.  
  9. >>>AttributeError: createList intance has no attribute '_listInt'
  10.  
You spelled your method wrong. It shuold be:
Expand|Select|Wrap|Line Numbers
  1. def __init__(self):
  2.  
Oct 15 '07 #2
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?
Expand|Select|Wrap|Line Numbers
  1. a=createList()
  2. a=addValue(2)
  3.  
  4. >> 2 in a
  5. >>error
Oct 15 '07 #3
ilikepython
844 Expert 512MB
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?
Expand|Select|Wrap|Line Numbers
  1. a=createList()
  2. a=addValue(2)
  3.  
  4. >> 2 in a
  5. >>error
You overload the in operator:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     def __contains__(self, obj):
  3.         # do whatever you want and return True or False
  4.  
Oct 16 '07 #4
You overload the in operator:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     def __contains__(self, obj):
  3.         # do whatever you want and return True or False
  4.  
First time see this, thank you very much ^_^

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

Expand|Select|Wrap|Line Numbers
  1. def __addValue(self, newValue):
  2.  
  3. # I can only add 1, however I want to do ListA.addvalue(2,3,4,5...) 
  4.  
  5.  
Oct 16 '07 #5
elcron
43
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?

Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __ini__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._ListInt.append(newvalue)
  7.  
  8.  
  9. >>>AttributeError: createList intance has no attribute '_listInt'
  10.  
Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def contains(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. test.contains(someItem)
  13.  
Or operator overloading
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def __contains__(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. someItem in test
  13.  
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.
Oct 16 '07 #6
Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def contains(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. test.contains(someItem)
  13.  
Or operator overloading
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def __contains__(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. someItem in test
  13.  
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
Oct 16 '07 #7
bvdet
2,851 Expert Mod 2GB
Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like:
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def contains(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. test.contains(someItem)
  13.  
Or operator overloading
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self):
  3.            self._listInt = list()
  4.  
  5.       def __addValue(self, newValue):
  6.            self._listInt.append(newvalue)
  7.  
  8.       def __contains__(self, item):
  9.             return item in self.listInt
  10.  
  11. test = createList()
  12. someItem in test
  13.  
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.
Expand|Select|Wrap|Line Numbers
  1. class createList:
  2.       def __init__(self, *args):
  3.            self._listInt = list(args)
  4.  
  5.       def __addValue(self, *newValue):
  6.            self._listInt.extend(newValue)
  7.  
  8.       def contains(self, item):
  9.             return item in self._listInt
  10.  
  11. test = createList(9,2,3,6,8)
  12. print test._listInt
  13. test._createList__addValue(21,22,23)
  14. print test._listInt
  15. print test.contains(9)
  16.  
  17.  
  18. class createList:
  19.       def __init__(self, *args):
  20.            self._listInt = list(args)
  21.  
  22.       def __addValue(self, *newValue):
  23.            self._listInt.extend(newValue)
  24.  
  25.       def __contains__(self, item):
  26.             return item in self._listInt
  27.  
  28. test = createList(22,23,24)
  29. print test._listInt
  30. test._createList__addValue(3,4,5,6)
  31. print test._listInt
  32. test._createList__addValue(36)
  33. print test._listInt
  34. print 36 in test
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
>>>
Oct 16 '07 #8

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

Similar topics

10
by: Joe Laughlin | last post by:
I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking? Example: I want to define a new type that's like a double, except that you can only give...
5
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
2
by: SA | last post by:
Hi all, How could I go about defining ctors in an interface. (not to instantiate the interface, of course, but to force the implementing class to provide a specific ctor). Sub New(ByVal x as...
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
2
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the...
4
by: Mark Ingram | last post by:
Hi, does anyone know if it is possible to define the function body of a getter and setter in the .cpp file? I have tried this, but it doesn't work (Error 1 error C2039:...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
6
by: Wayne Shu | last post by:
hi everyone! I have a problem in implementing a common class interface. my assignment is to implement a data structure list, and I have define a class template list_base, it's an abstract class,...
2
by: Martin Z | last post by:
Hi, I'm trying to support a 3rd-party XML format for which there is no schema. As such, I've been making my objects that map to their format using the XMLSerializer... but there's one object...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.