TypeError : takes 0 positional arguments but 1 was given [duplicate]
This question already has an answer here:
TypeError: worker() takes 0 positional arguments but 1 was given
4 answers
I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .
Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"
Below is the code which i am using :
class myFirst:
def first_func():
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
python python-3.x
marked as duplicate by Jean-François Fabre
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 20:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
TypeError: worker() takes 0 positional arguments but 1 was given
4 answers
I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .
Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"
Below is the code which i am using :
class myFirst:
def first_func():
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
python python-3.x
marked as duplicate by Jean-François Fabre
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 20:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
TypeError: worker() takes 0 positional arguments but 1 was given
4 answers
I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .
Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"
Below is the code which i am using :
class myFirst:
def first_func():
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
python python-3.x
This question already has an answer here:
TypeError: worker() takes 0 positional arguments but 1 was given
4 answers
I have started learning Python. I have created a class which has a function and in that function i have a dictionary . No i am checking whether a key exists in the dictionary or not , after checking i am returning a value from the function .
Now i cam trying to access the function but when when i am doing so i am getting an error "TypeError: first_func() takes 0 positional arguments but 1 was given"
Below is the code which i am using :
class myFirst:
def first_func():
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
This question already has an answer here:
TypeError: worker() takes 0 positional arguments but 1 was given
4 answers
python python-3.x
python python-3.x
asked Nov 12 at 20:34
Developer
3,557174485
3,557174485
marked as duplicate by Jean-François Fabre
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 20:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jean-François Fabre
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 20:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
methods (functions or procedures belonging to a class) require a self
argument (you may be familiar with this
from other languages) like so:
class myFirst:
def first_func(self): # here
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
it should work with that minor change
thanks for explaining on this
– Developer
Nov 12 at 20:49
add a comment |
The error is you're not referencing self in your function signature. Change the signature to
def first_func(self):
# rest of code
add a comment |
In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status
, and then you compared status to an int. Instead, you can do something like this to save a few lines-
myClassObj = myFirst()
if myClassObj.first_func():
print "Pass"
else
print "Fail"
The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
methods (functions or procedures belonging to a class) require a self
argument (you may be familiar with this
from other languages) like so:
class myFirst:
def first_func(self): # here
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
it should work with that minor change
thanks for explaining on this
– Developer
Nov 12 at 20:49
add a comment |
methods (functions or procedures belonging to a class) require a self
argument (you may be familiar with this
from other languages) like so:
class myFirst:
def first_func(self): # here
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
it should work with that minor change
thanks for explaining on this
– Developer
Nov 12 at 20:49
add a comment |
methods (functions or procedures belonging to a class) require a self
argument (you may be familiar with this
from other languages) like so:
class myFirst:
def first_func(self): # here
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
it should work with that minor change
methods (functions or procedures belonging to a class) require a self
argument (you may be familiar with this
from other languages) like so:
class myFirst:
def first_func(self): # here
flag=0
phonebook =
"A" : 938477566,
"B" : 938377264,
"C" : 947662781
# testing code
if "A" in phonebook:
flag=1
if "D" not in phonebook:
flag = 0
return flag
myclassObj = myFirst()
status = myclassObj.first_func()
if status > 1:
print ("Pass")
else:
print ("fail")
it should work with that minor change
answered Nov 12 at 20:36
vencaslac
1,002217
1,002217
thanks for explaining on this
– Developer
Nov 12 at 20:49
add a comment |
thanks for explaining on this
– Developer
Nov 12 at 20:49
thanks for explaining on this
– Developer
Nov 12 at 20:49
thanks for explaining on this
– Developer
Nov 12 at 20:49
add a comment |
The error is you're not referencing self in your function signature. Change the signature to
def first_func(self):
# rest of code
add a comment |
The error is you're not referencing self in your function signature. Change the signature to
def first_func(self):
# rest of code
add a comment |
The error is you're not referencing self in your function signature. Change the signature to
def first_func(self):
# rest of code
The error is you're not referencing self in your function signature. Change the signature to
def first_func(self):
# rest of code
answered Nov 12 at 20:37
Mark Xavier
161
161
add a comment |
add a comment |
In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status
, and then you compared status to an int. Instead, you can do something like this to save a few lines-
myClassObj = myFirst()
if myClassObj.first_func():
print "Pass"
else
print "Fail"
The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.
add a comment |
In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status
, and then you compared status to an int. Instead, you can do something like this to save a few lines-
myClassObj = myFirst()
if myClassObj.first_func():
print "Pass"
else
print "Fail"
The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.
add a comment |
In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status
, and then you compared status to an int. Instead, you can do something like this to save a few lines-
myClassObj = myFirst()
if myClassObj.first_func():
print "Pass"
else
print "Fail"
The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.
In addition to adding the self argument, you could consider shortening your code as well. Your function returns a boolean value which you assigned to status
, and then you compared status to an int. Instead, you can do something like this to save a few lines-
myClassObj = myFirst()
if myClassObj.first_func():
print "Pass"
else
print "Fail"
The complexity gain is quite minimal, but there's no reason to assign the boolean value to another variable when you can just use the value that's returned by your function.
answered Nov 12 at 20:40
Hollywood
9211
9211
add a comment |
add a comment |