AttributeError: 'builtin_function_or_method' object has no attribute 'split'
Question:
I sprang up on some errors. I couldn’t solve it. Basically I’m splitting my input wherever ‘and’ & ‘or’ occurs and deleting sequences having x, not x in the same sub-list. My code looks like this
import string
class list(object):
def __init__(self, input):
self.input = input
def update_list(self):
new_list = [i.split("and") for i in input.split("or")]
print new_list
def reduced(self):
re_list = [m for m in new_list if not any("not" + m in new_list)]
print re_list
def main():
my_input = raw_input(" ")
print my_input
my_list = list(my_input)
my_list.update_list()
my_list.reduced()
if __name__ == '__main__':
main()
errors I got:
Traceback (most recent call last):
line 39, in <module>
main()
line 32, in main
my_list.update_list()
line 18, in update_list
new_list = [i.split("and") for i in input.split("or")]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
my input looks like this:
apple and berry or not apple and apple or banana and not papaya
desired output:
[['apple', 'berry'],['banana', 'not papaya']]
also I use Python 2.x series
I rectified the above when I replaced input with self.input in the update_list()
But I got new errors stating
re_list = [m for m in new_list if not any("not" + m in new_list)]
NameError: global name 'new_list' is not defined
Answers:
The word input
is a standard function in Python, you should avoid using it. However, in this case, within the update_list
method, you don’t mean input
you mean self.input
. Since you didn’t specify the self.
, it is finding the input
function in standard python and assuming you mean that. Thus your error that a
‘builtin_function_or_method’ object has no attribute ‘split’
since the builtin function input
does not have a attribute split
.
EDIT—-
Ok, I’ll give you more since you seem to still be struggling. If you want an object to retain a value from call to call, you need to use “self.” to update an object’s attributes. I’m a little vague on what you want reduced
to do, so I may have that wrong.
Note: for my own testing sanity I hard-coded the input, but you could put your raw_input()
statement back in.
Also, stay away from names that could clobber Python built-ins, such as “list” and “input”.
class MyList(object):
''' Also, classes are usually Capitalized. '''
def __init__(self, data):
self.raw_data = data
def update_list(self):
''' I added an additional loop in order to strip away spaces. '''
self.parsed_data = [[ j.strip() for j in i.split("and") ] for i in self.raw_data.split("or") ]
return self.parsed_data
def reduce_list(self):
self.reduced_data = [m for m in self.parsed_data if m[0] != "not "+m[1] and m[1] != "not "+ m[0]]
return self.reduced_data
def test_my_list():
input_data = """apple and berry or not apple and apple or banana and not papaya"""
print(input_data)
my_list = MyList(input_data)
print(my_list.update_list())
print(my_list.reduce_list())
>>> test_my_list()
apple and berry or not apple and apple or banana and not papaya
[['apple', 'berry'], ['not apple', 'apple'], ['banana', 'not papaya']]
[['apple', 'berry'], ['banana', 'not papaya']]
It should be input().split()
not input.split()
. Add the brackets to both input and split
I sprang up on some errors. I couldn’t solve it. Basically I’m splitting my input wherever ‘and’ & ‘or’ occurs and deleting sequences having x, not x in the same sub-list. My code looks like this
import string
class list(object):
def __init__(self, input):
self.input = input
def update_list(self):
new_list = [i.split("and") for i in input.split("or")]
print new_list
def reduced(self):
re_list = [m for m in new_list if not any("not" + m in new_list)]
print re_list
def main():
my_input = raw_input(" ")
print my_input
my_list = list(my_input)
my_list.update_list()
my_list.reduced()
if __name__ == '__main__':
main()
errors I got:
Traceback (most recent call last):
line 39, in <module>
main()
line 32, in main
my_list.update_list()
line 18, in update_list
new_list = [i.split("and") for i in input.split("or")]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
my input looks like this:
apple and berry or not apple and apple or banana and not papaya
desired output:
[['apple', 'berry'],['banana', 'not papaya']]
also I use Python 2.x series
I rectified the above when I replaced input with self.input in the update_list()
But I got new errors stating
re_list = [m for m in new_list if not any("not" + m in new_list)]
NameError: global name 'new_list' is not defined
The word input
is a standard function in Python, you should avoid using it. However, in this case, within the update_list
method, you don’t mean input
you mean self.input
. Since you didn’t specify the self.
, it is finding the input
function in standard python and assuming you mean that. Thus your error that a
‘builtin_function_or_method’ object has no attribute ‘split’
since the builtin function input
does not have a attribute split
.
EDIT—-
Ok, I’ll give you more since you seem to still be struggling. If you want an object to retain a value from call to call, you need to use “self.” to update an object’s attributes. I’m a little vague on what you want reduced
to do, so I may have that wrong.
Note: for my own testing sanity I hard-coded the input, but you could put your raw_input()
statement back in.
Also, stay away from names that could clobber Python built-ins, such as “list” and “input”.
class MyList(object):
''' Also, classes are usually Capitalized. '''
def __init__(self, data):
self.raw_data = data
def update_list(self):
''' I added an additional loop in order to strip away spaces. '''
self.parsed_data = [[ j.strip() for j in i.split("and") ] for i in self.raw_data.split("or") ]
return self.parsed_data
def reduce_list(self):
self.reduced_data = [m for m in self.parsed_data if m[0] != "not "+m[1] and m[1] != "not "+ m[0]]
return self.reduced_data
def test_my_list():
input_data = """apple and berry or not apple and apple or banana and not papaya"""
print(input_data)
my_list = MyList(input_data)
print(my_list.update_list())
print(my_list.reduce_list())
>>> test_my_list()
apple and berry or not apple and apple or banana and not papaya
[['apple', 'berry'], ['not apple', 'apple'], ['banana', 'not papaya']]
[['apple', 'berry'], ['banana', 'not papaya']]
It should be input().split()
not input.split()
. Add the brackets to both input and split