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? -
class createList:
-
def __ini__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._ListInt.append(newvalue)
-
-
-
>>>AttributeError: createList intance has no attribute '_listInt'
-
7 1513
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? -
class createList:
-
def __ini__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._ListInt.append(newvalue)
-
-
-
>>>AttributeError: createList intance has no attribute '_listInt'
-
You spelled your method wrong. It shuold be:
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? -
a=createList()
-
a=addValue(2)
-
-
>> 2 in a
-
>>error
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? -
a=createList()
-
a=addValue(2)
-
-
>> 2 in a
-
>>error
You overload the in operator: -
...
-
def __contains__(self, obj):
-
# do whatever you want and return True or False
-
You overload the in operator: -
...
-
def __contains__(self, obj):
-
# do whatever you want and return True or False
-
First time see this, thank you very much ^_^
anyway, do we have a way to add n integers into the list? If I use -
def __addValue(self, newValue):
-
-
# I can only add 1, however I want to do ListA.addvalue(2,3,4,5...)
-
-
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? -
class createList:
-
def __ini__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._ListInt.append(newvalue)
-
-
-
>>>AttributeError: createList intance has no attribute '_listInt'
-
Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like: -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def contains(self, item):
-
return item in self.listInt
-
-
test = createList()
-
test.contains(someItem)
-
Or operator overloading -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def __contains__(self, item):
-
return item in self.listInt
-
-
test = createList()
-
someItem in test
-
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.
Watch out for capitalization self.listInt does not equal self.ListInt. As for the function you can either write a function like: -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def contains(self, item):
-
return item in self.listInt
-
-
test = createList()
-
test.contains(someItem)
-
Or operator overloading -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def __contains__(self, item):
-
return item in self.listInt
-
-
test = createList()
-
someItem in test
-
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
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: -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def contains(self, item):
-
return item in self.listInt
-
-
test = createList()
-
test.contains(someItem)
-
Or operator overloading -
class createList:
-
def __init__(self):
-
self._listInt = list()
-
-
def __addValue(self, newValue):
-
self._listInt.append(newvalue)
-
-
def __contains__(self, item):
-
return item in self.listInt
-
-
test = createList()
-
someItem in test
-
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. - class createList:
-
def __init__(self, *args):
-
self._listInt = list(args)
-
-
def __addValue(self, *newValue):
-
self._listInt.extend(newValue)
-
-
def contains(self, item):
-
return item in self._listInt
-
-
test = createList(9,2,3,6,8)
-
print test._listInt
-
test._createList__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.extend(newValue)
-
-
def __contains__(self, item):
-
return item in self._listInt
-
-
test = createList(22,23,24)
-
print test._listInt
-
test._createList__addValue(3,4,5,6)
-
print test._listInt
-
test._createList__addValue(36)
-
print test._listInt
-
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
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
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...
|
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...
|
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...
|
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:...
|
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
|
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,...
|
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...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |