Treat a single element list as a scalar in Python












0















¿Is it possible to define a subclass of a python list which allow the use of single-element list variables as if they were scalars?



For example I would like to be able to do:



class CustomList(list):
...
...

list1 = CustomList([2])
list2 = CustomList([3])
list3 = CustomList([4,5,6])
list4 = CustomList([{'foo':'bar'}])
list1 #should return list1[0]
list4['foo'] #should return list4[0]['foo'] = 'bar'
list1 + list2 #should return list1[0] + list2[0] = 5


but keeping the ability to use the list as normal:



for i in list3:
print list[i]









share|improve this question




















  • 1





    What do you hope the addition method does? Concatenation like for a list?

    – jwil
    Nov 13 '18 at 20:09











  • Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

    – MisterMiyagi
    Nov 13 '18 at 20:43











  • @MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

    – Manuel
    Nov 13 '18 at 20:54













  • I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

    – MisterMiyagi
    Nov 13 '18 at 21:17











  • @MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

    – Manuel
    Nov 13 '18 at 22:02


















0















¿Is it possible to define a subclass of a python list which allow the use of single-element list variables as if they were scalars?



For example I would like to be able to do:



class CustomList(list):
...
...

list1 = CustomList([2])
list2 = CustomList([3])
list3 = CustomList([4,5,6])
list4 = CustomList([{'foo':'bar'}])
list1 #should return list1[0]
list4['foo'] #should return list4[0]['foo'] = 'bar'
list1 + list2 #should return list1[0] + list2[0] = 5


but keeping the ability to use the list as normal:



for i in list3:
print list[i]









share|improve this question




















  • 1





    What do you hope the addition method does? Concatenation like for a list?

    – jwil
    Nov 13 '18 at 20:09











  • Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

    – MisterMiyagi
    Nov 13 '18 at 20:43











  • @MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

    – Manuel
    Nov 13 '18 at 20:54













  • I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

    – MisterMiyagi
    Nov 13 '18 at 21:17











  • @MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

    – Manuel
    Nov 13 '18 at 22:02
















0












0








0








¿Is it possible to define a subclass of a python list which allow the use of single-element list variables as if they were scalars?



For example I would like to be able to do:



class CustomList(list):
...
...

list1 = CustomList([2])
list2 = CustomList([3])
list3 = CustomList([4,5,6])
list4 = CustomList([{'foo':'bar'}])
list1 #should return list1[0]
list4['foo'] #should return list4[0]['foo'] = 'bar'
list1 + list2 #should return list1[0] + list2[0] = 5


but keeping the ability to use the list as normal:



for i in list3:
print list[i]









share|improve this question
















¿Is it possible to define a subclass of a python list which allow the use of single-element list variables as if they were scalars?



For example I would like to be able to do:



class CustomList(list):
...
...

list1 = CustomList([2])
list2 = CustomList([3])
list3 = CustomList([4,5,6])
list4 = CustomList([{'foo':'bar'}])
list1 #should return list1[0]
list4['foo'] #should return list4[0]['foo'] = 'bar'
list1 + list2 #should return list1[0] + list2[0] = 5


but keeping the ability to use the list as normal:



for i in list3:
print list[i]






python list subclass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 23:25







Manuel

















asked Nov 13 '18 at 20:07









ManuelManuel

32




32








  • 1





    What do you hope the addition method does? Concatenation like for a list?

    – jwil
    Nov 13 '18 at 20:09











  • Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

    – MisterMiyagi
    Nov 13 '18 at 20:43











  • @MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

    – Manuel
    Nov 13 '18 at 20:54













  • I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

    – MisterMiyagi
    Nov 13 '18 at 21:17











  • @MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

    – Manuel
    Nov 13 '18 at 22:02
















  • 1





    What do you hope the addition method does? Concatenation like for a list?

    – jwil
    Nov 13 '18 at 20:09











  • Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

    – MisterMiyagi
    Nov 13 '18 at 20:43











  • @MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

    – Manuel
    Nov 13 '18 at 20:54













  • I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

    – MisterMiyagi
    Nov 13 '18 at 21:17











  • @MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

    – Manuel
    Nov 13 '18 at 22:02










1




1





What do you hope the addition method does? Concatenation like for a list?

– jwil
Nov 13 '18 at 20:09





What do you hope the addition method does? Concatenation like for a list?

– jwil
Nov 13 '18 at 20:09













Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

– MisterMiyagi
Nov 13 '18 at 20:43





Defining a class CustomList(list) is exactly what is needed, what problem did you encounter?

– MisterMiyagi
Nov 13 '18 at 20:43













@MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

– Manuel
Nov 13 '18 at 20:54







@MisterMiyagi "list1 + list2" should return the sum of the values of list1[0] and list2[0]. See my last edit.

– Manuel
Nov 13 '18 at 20:54















I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

– MisterMiyagi
Nov 13 '18 at 21:17





I see what you want, but not what your problem is. Do you not know how to overload operators? How to switch depending on the length of lists? How to use the base class behaviour for non-scalar lists?

– MisterMiyagi
Nov 13 '18 at 21:17













@MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

– Manuel
Nov 13 '18 at 22:02







@MisterMiyagi I do not have experience defining classes but I can adapt Green Cloak Guy example for other binary operators. However I do not know what method to redefine (or what operator to overload) to use for example "print(list)" instead of "print(list[0])".

– Manuel
Nov 13 '18 at 22:02














2 Answers
2






active

oldest

votes


















2














Yes, but you would effectively need to override any methods you'd want it to work with.



When you call certain operators on objects like lists, you end up calling hidden methods on those objects. In the example you gave, result = list1 + list2, the function that is actually called is list1.__add__(list2). You can subclass list and override these methods, if you want. For example:



class CustomList(list):
def __add__(self, value):
if len(self) == 1 and len(value) == 1:
return self[0] + value[0]
else:
return CustomList(list.__add__(self, value))


will treat list1 and list2 as scalars if they're both of length 1, and refer to the normal list functionality otherwise. This stackoverflow answer on subclassing list might be helpful to you.





To address your edit, a more general solution would be fairly simple - instead of overriding the __add__() function, try overriding __getitem__(), which is called whenever you use the square-bracket operators:



class CustomList(list):
def __getitem__(self, y):
if len(self) == 1:
return self[0][y]
else:
return self[y]


This might cause problems when trying to concatenate arrays, however; list3 + list1 would cause an error because list1 was not an iterable, though of course you could override __add__ to simply add the second value as a list element if the list was more than one element long.





Console output using the above declaration:



>>> list1 = CustomList([2])
>>> list2 = CustomList([3])
>>> list3 = CustomList([4,5,6])
>>> print(list1 + list2)
5
>>> print(list2 + list3)
[3, 4, 5, 6]





share|improve this answer





















  • 1





    Addition should probably return another CustomList, not a plain list.

    – MisterMiyagi
    Nov 13 '18 at 21:18











  • Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

    – Manuel
    Nov 13 '18 at 21:19













  • If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

    – Green Cloak Guy
    Nov 14 '18 at 2:55











  • @GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

    – Manuel
    Nov 14 '18 at 6:24





















0














Assuming by adding two classes you meant concatenating two lists, you could do something like this (notice the __add__ method to see how to addition works):



from collections import MutableSequence

class CustomList(MutableSequence):
def __init__(self, data=):
self._list = data

def __add__(self, newlist):
return self._list + newlist._list

def __len__(self):
return len(self._list)

def __getitem__(self, i):
return self._list[i]

def __delitem__(self, i):
del self._list[i]

def __setitem__(self, i, val):
self._list[i] = val

def __str__(self):
return str(self._list)

def insert(self, i, val):
self._list.insert(i, val)





share|improve this answer
























  • list can be subclassed directly, there is no need to write all that boilerplate.

    – MisterMiyagi
    Nov 13 '18 at 20:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288712%2ftreat-a-single-element-list-as-a-scalar-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Yes, but you would effectively need to override any methods you'd want it to work with.



When you call certain operators on objects like lists, you end up calling hidden methods on those objects. In the example you gave, result = list1 + list2, the function that is actually called is list1.__add__(list2). You can subclass list and override these methods, if you want. For example:



class CustomList(list):
def __add__(self, value):
if len(self) == 1 and len(value) == 1:
return self[0] + value[0]
else:
return CustomList(list.__add__(self, value))


will treat list1 and list2 as scalars if they're both of length 1, and refer to the normal list functionality otherwise. This stackoverflow answer on subclassing list might be helpful to you.





To address your edit, a more general solution would be fairly simple - instead of overriding the __add__() function, try overriding __getitem__(), which is called whenever you use the square-bracket operators:



class CustomList(list):
def __getitem__(self, y):
if len(self) == 1:
return self[0][y]
else:
return self[y]


This might cause problems when trying to concatenate arrays, however; list3 + list1 would cause an error because list1 was not an iterable, though of course you could override __add__ to simply add the second value as a list element if the list was more than one element long.





Console output using the above declaration:



>>> list1 = CustomList([2])
>>> list2 = CustomList([3])
>>> list3 = CustomList([4,5,6])
>>> print(list1 + list2)
5
>>> print(list2 + list3)
[3, 4, 5, 6]





share|improve this answer





















  • 1





    Addition should probably return another CustomList, not a plain list.

    – MisterMiyagi
    Nov 13 '18 at 21:18











  • Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

    – Manuel
    Nov 13 '18 at 21:19













  • If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

    – Green Cloak Guy
    Nov 14 '18 at 2:55











  • @GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

    – Manuel
    Nov 14 '18 at 6:24


















2














Yes, but you would effectively need to override any methods you'd want it to work with.



When you call certain operators on objects like lists, you end up calling hidden methods on those objects. In the example you gave, result = list1 + list2, the function that is actually called is list1.__add__(list2). You can subclass list and override these methods, if you want. For example:



class CustomList(list):
def __add__(self, value):
if len(self) == 1 and len(value) == 1:
return self[0] + value[0]
else:
return CustomList(list.__add__(self, value))


will treat list1 and list2 as scalars if they're both of length 1, and refer to the normal list functionality otherwise. This stackoverflow answer on subclassing list might be helpful to you.





To address your edit, a more general solution would be fairly simple - instead of overriding the __add__() function, try overriding __getitem__(), which is called whenever you use the square-bracket operators:



class CustomList(list):
def __getitem__(self, y):
if len(self) == 1:
return self[0][y]
else:
return self[y]


This might cause problems when trying to concatenate arrays, however; list3 + list1 would cause an error because list1 was not an iterable, though of course you could override __add__ to simply add the second value as a list element if the list was more than one element long.





Console output using the above declaration:



>>> list1 = CustomList([2])
>>> list2 = CustomList([3])
>>> list3 = CustomList([4,5,6])
>>> print(list1 + list2)
5
>>> print(list2 + list3)
[3, 4, 5, 6]





share|improve this answer





















  • 1





    Addition should probably return another CustomList, not a plain list.

    – MisterMiyagi
    Nov 13 '18 at 21:18











  • Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

    – Manuel
    Nov 13 '18 at 21:19













  • If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

    – Green Cloak Guy
    Nov 14 '18 at 2:55











  • @GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

    – Manuel
    Nov 14 '18 at 6:24
















2












2








2







Yes, but you would effectively need to override any methods you'd want it to work with.



When you call certain operators on objects like lists, you end up calling hidden methods on those objects. In the example you gave, result = list1 + list2, the function that is actually called is list1.__add__(list2). You can subclass list and override these methods, if you want. For example:



class CustomList(list):
def __add__(self, value):
if len(self) == 1 and len(value) == 1:
return self[0] + value[0]
else:
return CustomList(list.__add__(self, value))


will treat list1 and list2 as scalars if they're both of length 1, and refer to the normal list functionality otherwise. This stackoverflow answer on subclassing list might be helpful to you.





To address your edit, a more general solution would be fairly simple - instead of overriding the __add__() function, try overriding __getitem__(), which is called whenever you use the square-bracket operators:



class CustomList(list):
def __getitem__(self, y):
if len(self) == 1:
return self[0][y]
else:
return self[y]


This might cause problems when trying to concatenate arrays, however; list3 + list1 would cause an error because list1 was not an iterable, though of course you could override __add__ to simply add the second value as a list element if the list was more than one element long.





Console output using the above declaration:



>>> list1 = CustomList([2])
>>> list2 = CustomList([3])
>>> list3 = CustomList([4,5,6])
>>> print(list1 + list2)
5
>>> print(list2 + list3)
[3, 4, 5, 6]





share|improve this answer















Yes, but you would effectively need to override any methods you'd want it to work with.



When you call certain operators on objects like lists, you end up calling hidden methods on those objects. In the example you gave, result = list1 + list2, the function that is actually called is list1.__add__(list2). You can subclass list and override these methods, if you want. For example:



class CustomList(list):
def __add__(self, value):
if len(self) == 1 and len(value) == 1:
return self[0] + value[0]
else:
return CustomList(list.__add__(self, value))


will treat list1 and list2 as scalars if they're both of length 1, and refer to the normal list functionality otherwise. This stackoverflow answer on subclassing list might be helpful to you.





To address your edit, a more general solution would be fairly simple - instead of overriding the __add__() function, try overriding __getitem__(), which is called whenever you use the square-bracket operators:



class CustomList(list):
def __getitem__(self, y):
if len(self) == 1:
return self[0][y]
else:
return self[y]


This might cause problems when trying to concatenate arrays, however; list3 + list1 would cause an error because list1 was not an iterable, though of course you could override __add__ to simply add the second value as a list element if the list was more than one element long.





Console output using the above declaration:



>>> list1 = CustomList([2])
>>> list2 = CustomList([3])
>>> list3 = CustomList([4,5,6])
>>> print(list1 + list2)
5
>>> print(list2 + list3)
[3, 4, 5, 6]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 2:54

























answered Nov 13 '18 at 20:28









Green Cloak GuyGreen Cloak Guy

2,5031720




2,5031720








  • 1





    Addition should probably return another CustomList, not a plain list.

    – MisterMiyagi
    Nov 13 '18 at 21:18











  • Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

    – Manuel
    Nov 13 '18 at 21:19













  • If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

    – Green Cloak Guy
    Nov 14 '18 at 2:55











  • @GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

    – Manuel
    Nov 14 '18 at 6:24
















  • 1





    Addition should probably return another CustomList, not a plain list.

    – MisterMiyagi
    Nov 13 '18 at 21:18











  • Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

    – Manuel
    Nov 13 '18 at 21:19













  • If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

    – Green Cloak Guy
    Nov 14 '18 at 2:55











  • @GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

    – Manuel
    Nov 14 '18 at 6:24










1




1





Addition should probably return another CustomList, not a plain list.

– MisterMiyagi
Nov 13 '18 at 21:18





Addition should probably return another CustomList, not a plain list.

– MisterMiyagi
Nov 13 '18 at 21:18













Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

– Manuel
Nov 13 '18 at 21:19







Thank you, this is exactly what I want to do with arithmetic operators, but still, I do not have idea of what to with something like: list4 = "CustomList[{'foo':'bar'}]" and then do "print(list4['foo'])" instead of "print(list4[0]['foo'])". See my edit.

– Manuel
Nov 13 '18 at 21:19















If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

– Green Cloak Guy
Nov 14 '18 at 2:55





If you wanted to make that work like a dictionary, then you could also override the __getitem__() method, which activates whenever you use the square-bracket operators. I edited a simple example into my post, though of course you should do whatever you need to make it fit your implementation

– Green Cloak Guy
Nov 14 '18 at 2:55













@GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

– Manuel
Nov 14 '18 at 6:24







@GreenCloakGuy Great! I think I can work with your last example. List concatenation is not an issue because I'm not going to use the subclass as replacement of the standard list class.

– Manuel
Nov 14 '18 at 6:24















0














Assuming by adding two classes you meant concatenating two lists, you could do something like this (notice the __add__ method to see how to addition works):



from collections import MutableSequence

class CustomList(MutableSequence):
def __init__(self, data=):
self._list = data

def __add__(self, newlist):
return self._list + newlist._list

def __len__(self):
return len(self._list)

def __getitem__(self, i):
return self._list[i]

def __delitem__(self, i):
del self._list[i]

def __setitem__(self, i, val):
self._list[i] = val

def __str__(self):
return str(self._list)

def insert(self, i, val):
self._list.insert(i, val)





share|improve this answer
























  • list can be subclassed directly, there is no need to write all that boilerplate.

    – MisterMiyagi
    Nov 13 '18 at 20:36
















0














Assuming by adding two classes you meant concatenating two lists, you could do something like this (notice the __add__ method to see how to addition works):



from collections import MutableSequence

class CustomList(MutableSequence):
def __init__(self, data=):
self._list = data

def __add__(self, newlist):
return self._list + newlist._list

def __len__(self):
return len(self._list)

def __getitem__(self, i):
return self._list[i]

def __delitem__(self, i):
del self._list[i]

def __setitem__(self, i, val):
self._list[i] = val

def __str__(self):
return str(self._list)

def insert(self, i, val):
self._list.insert(i, val)





share|improve this answer
























  • list can be subclassed directly, there is no need to write all that boilerplate.

    – MisterMiyagi
    Nov 13 '18 at 20:36














0












0








0







Assuming by adding two classes you meant concatenating two lists, you could do something like this (notice the __add__ method to see how to addition works):



from collections import MutableSequence

class CustomList(MutableSequence):
def __init__(self, data=):
self._list = data

def __add__(self, newlist):
return self._list + newlist._list

def __len__(self):
return len(self._list)

def __getitem__(self, i):
return self._list[i]

def __delitem__(self, i):
del self._list[i]

def __setitem__(self, i, val):
self._list[i] = val

def __str__(self):
return str(self._list)

def insert(self, i, val):
self._list.insert(i, val)





share|improve this answer













Assuming by adding two classes you meant concatenating two lists, you could do something like this (notice the __add__ method to see how to addition works):



from collections import MutableSequence

class CustomList(MutableSequence):
def __init__(self, data=):
self._list = data

def __add__(self, newlist):
return self._list + newlist._list

def __len__(self):
return len(self._list)

def __getitem__(self, i):
return self._list[i]

def __delitem__(self, i):
del self._list[i]

def __setitem__(self, i, val):
self._list[i] = val

def __str__(self):
return str(self._list)

def insert(self, i, val):
self._list.insert(i, val)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 20:30









Tadej MagajnaTadej Magajna

1,1201232




1,1201232













  • list can be subclassed directly, there is no need to write all that boilerplate.

    – MisterMiyagi
    Nov 13 '18 at 20:36



















  • list can be subclassed directly, there is no need to write all that boilerplate.

    – MisterMiyagi
    Nov 13 '18 at 20:36

















list can be subclassed directly, there is no need to write all that boilerplate.

– MisterMiyagi
Nov 13 '18 at 20:36





list can be subclassed directly, there is no need to write all that boilerplate.

– MisterMiyagi
Nov 13 '18 at 20:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288712%2ftreat-a-single-element-list-as-a-scalar-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python