Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python nonetype variable
#1
hello I have the following in a threaded function executes several at the same time
profit is global variable

profit += result
profit = round (profit, 2)

and there is no way to get it when you run 2 simultaneous error

python TypeError: unsupported operand type (s) for +: 'int' and 'NoneType'

what can I do

if profit is None:

profit = 0

did not solve my problem, the profit variable is profit = 0 before the function and does not work if it is not simultaneous, only 2 theads calling the function there of the error
Reply
#2
you could use:
try:
    profit += result
except TypeError:
    profit = 0
But it's much better pre-initialize both variables to zero:

result = 0
profit = 0
# ... do some stuff
profit += result
[/python]
Reply
#3
(Nov-12-2020, 01:35 AM)Larz60+ Wrote: you could use:
try:
    profit += result
except TypeError:
    profit = 0
But it's much better pre-initialize both variables to zero:

result = 0
profit = 0
# ... do some stuff
profit += result
[/python]


[python]
result = 0
profit = 0


this treads dont set 0, is SUM of the 2 funciton simultaneos
Reply
#4
Not sure what you're asking.
Reply
#5
Quote:python TypeError: unsupported operand type (s) for +: 'int' and 'NoneType'

Note the order. "int" is first and "NoneType" is second. That reflects the order of the expression as well.

Quote:profit += result

If it's for this line, then it is result that is none in the expression, not profit.
Reply
#6
you should always initialize variables to some finite state.

or if not, at least capture any exceptions, otherwise at some point your program may crash and it may only do so when something very unusual happens, perhaps several months after software has been released.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,491 Jun-28-2023, 11:13 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020