data type is not recognizable using Threads in Python












1














I am trying to make a calculation code using threads in Python. My code is the following:



from threading import Thread
from time import sleep

class Fact(Thread):
def __init__(self, start, end):
Thread.__init__(self)
self.start = start
self.end = end

def factorial(self):
p = 1
for i in range(self.start, self.end):
p = p * i
return p

def run(self):
global s
s = s + self.factorial()
sleep(2)
print ("output is ", s)

s = 0

def main():
n = 6
mid = n / 2
obj1 = Fact(1, mid)
obj1.start()
obj2 = Fact(mid + 1, n)
obj2.start()

if __name__ == "__main__":
main()


What I want is to compute, iteratively, the factorial of a number. For example, if I enter 6 as an input it should do the following products:



product(1, 3) + product(4, 6)


The problem is that when I run this code:



obj1.start()


I got the following error:



TypeError: 'int' object is not callable


I cannot find which one could be the mistake.










share|improve this question




















  • 1




    You're trying to use start for a method and an instance variable at the same time.
    – user2357112
    Nov 20 at 18:29






  • 1




    There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
    – Austin
    Nov 20 at 18:30












  • obj1.start is not a function - did you mean obj1.run() instead?
    – Robin Zigmond
    Nov 20 at 18:31






  • 3




    BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
    – zvone
    Nov 20 at 18:35
















1














I am trying to make a calculation code using threads in Python. My code is the following:



from threading import Thread
from time import sleep

class Fact(Thread):
def __init__(self, start, end):
Thread.__init__(self)
self.start = start
self.end = end

def factorial(self):
p = 1
for i in range(self.start, self.end):
p = p * i
return p

def run(self):
global s
s = s + self.factorial()
sleep(2)
print ("output is ", s)

s = 0

def main():
n = 6
mid = n / 2
obj1 = Fact(1, mid)
obj1.start()
obj2 = Fact(mid + 1, n)
obj2.start()

if __name__ == "__main__":
main()


What I want is to compute, iteratively, the factorial of a number. For example, if I enter 6 as an input it should do the following products:



product(1, 3) + product(4, 6)


The problem is that when I run this code:



obj1.start()


I got the following error:



TypeError: 'int' object is not callable


I cannot find which one could be the mistake.










share|improve this question




















  • 1




    You're trying to use start for a method and an instance variable at the same time.
    – user2357112
    Nov 20 at 18:29






  • 1




    There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
    – Austin
    Nov 20 at 18:30












  • obj1.start is not a function - did you mean obj1.run() instead?
    – Robin Zigmond
    Nov 20 at 18:31






  • 3




    BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
    – zvone
    Nov 20 at 18:35














1












1








1







I am trying to make a calculation code using threads in Python. My code is the following:



from threading import Thread
from time import sleep

class Fact(Thread):
def __init__(self, start, end):
Thread.__init__(self)
self.start = start
self.end = end

def factorial(self):
p = 1
for i in range(self.start, self.end):
p = p * i
return p

def run(self):
global s
s = s + self.factorial()
sleep(2)
print ("output is ", s)

s = 0

def main():
n = 6
mid = n / 2
obj1 = Fact(1, mid)
obj1.start()
obj2 = Fact(mid + 1, n)
obj2.start()

if __name__ == "__main__":
main()


What I want is to compute, iteratively, the factorial of a number. For example, if I enter 6 as an input it should do the following products:



product(1, 3) + product(4, 6)


The problem is that when I run this code:



obj1.start()


I got the following error:



TypeError: 'int' object is not callable


I cannot find which one could be the mistake.










share|improve this question















I am trying to make a calculation code using threads in Python. My code is the following:



from threading import Thread
from time import sleep

class Fact(Thread):
def __init__(self, start, end):
Thread.__init__(self)
self.start = start
self.end = end

def factorial(self):
p = 1
for i in range(self.start, self.end):
p = p * i
return p

def run(self):
global s
s = s + self.factorial()
sleep(2)
print ("output is ", s)

s = 0

def main():
n = 6
mid = n / 2
obj1 = Fact(1, mid)
obj1.start()
obj2 = Fact(mid + 1, n)
obj2.start()

if __name__ == "__main__":
main()


What I want is to compute, iteratively, the factorial of a number. For example, if I enter 6 as an input it should do the following products:



product(1, 3) + product(4, 6)


The problem is that when I run this code:



obj1.start()


I got the following error:



TypeError: 'int' object is not callable


I cannot find which one could be the mistake.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 18:57









David the third

1,1681721




1,1681721










asked Nov 20 at 18:26









Little

94052145




94052145








  • 1




    You're trying to use start for a method and an instance variable at the same time.
    – user2357112
    Nov 20 at 18:29






  • 1




    There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
    – Austin
    Nov 20 at 18:30












  • obj1.start is not a function - did you mean obj1.run() instead?
    – Robin Zigmond
    Nov 20 at 18:31






  • 3




    BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
    – zvone
    Nov 20 at 18:35














  • 1




    You're trying to use start for a method and an instance variable at the same time.
    – user2357112
    Nov 20 at 18:29






  • 1




    There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
    – Austin
    Nov 20 at 18:30












  • obj1.start is not a function - did you mean obj1.run() instead?
    – Robin Zigmond
    Nov 20 at 18:31






  • 3




    BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
    – zvone
    Nov 20 at 18:35








1




1




You're trying to use start for a method and an instance variable at the same time.
– user2357112
Nov 20 at 18:29




You're trying to use start for a method and an instance variable at the same time.
– user2357112
Nov 20 at 18:29




1




1




There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
– Austin
Nov 20 at 18:30






There is an instance variable in the same name (start) which is 'int' and 'int' is not callable. When you do obj1.start, it's actually referring to that instance variable.
– Austin
Nov 20 at 18:30














obj1.start is not a function - did you mean obj1.run() instead?
– Robin Zigmond
Nov 20 at 18:31




obj1.start is not a function - did you mean obj1.run() instead?
– Robin Zigmond
Nov 20 at 18:31




3




3




BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
– zvone
Nov 20 at 18:35




BTW, while using global variables is always a bad idea, in this case it is even worse. You are using multiple threads to modify the same global variable. This can cause you pretty bad errors if one thread modifies the variable while the other one is using it.
– zvone
Nov 20 at 18:35

















active

oldest

votes











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%2f53399269%2fdata-type-is-not-recognizable-using-threads-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53399269%2fdata-type-is-not-recognizable-using-threads-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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'