Posts: 256
Threads: 60
Joined: Apr 2019
Apr-01-2022, 10:04 PM
(This post was last modified: Apr-03-2022, 03:17 PM by paul18fr.)
Hi,
I'm spending hours in understanding why a 'None' appears immediatly after creating a dictionary, leading to a crash afterward (such dictionary is created in a function and returned)
Here is a basic example without any 'None'
import numpy as np
Mat = np.random.random( (2, 2) )
MyDict = {}
print(MyDict)
MyDict.update({'Mat': Mat},)
DictList = list(MyDict.keys())
def test(A):
DefDict = {}
print(DefDict)
DefDict.update({'A': A})
return DefDict
B = test(Mat) Output: {}
{}
In my real case:
RealDict = {}
print(RealDict) Output: {}
None
That's the first time the dictionary is created: any idea to explain from where 'None' comes from?
Thanks
Paul
Posts: 11,873
Threads: 474
Joined: Sep 2016
Apr-01-2022, 11:49 PM
(This post was last modified: Apr-01-2022, 11:54 PM by Larz60+.)
MyDict = {} Creates an empty dictionary, thus None is it's value until you actually add items.
>>> MyDict = {}
>>> print(MyDict)
{}
>>> key = 2
>>> kvalue = 2
>>> MyDict[key] = kvalue
>>> print(MyDict)
{2: 2} You can then add additional data
>>> another_key = 'Food'
>>> another_value = 'Banana'
>>> MyDict[another_key] = another_value
>>> print(MyDict)
{2: 2, 'Food': 'Banana'}
>>>
>>> print(MyDict['Food'])
Banana
>>>
Posts: 256
Threads: 60
Joined: Apr 2019
Thanks Larz60+
Nonetheless the trouble I'm facing comes from the "none" into the dictionary (my code crashes because of it and because it's not supossed to be there: either the dictionary is empty, either it's composed of array).
Of course, I guessed it comes from my initialization, but I'm still wondering why?
Curently my workflow has been:
- the dictionary is initialized (remains empty accordingly)
- when specific conditions are reached, then it is updated (both keys and values are "variables")
- the content of the dictionary is listed and additional calculations on arrays are performed ((but the None appears here)
Another way would be to create the dictionary when the first the condition is met, but it request to be able to test is the dictionary exists, and I still don't know how (may be a bit naive)?
I guess I'm still missing something, but I'm looking into a way to fix it
Paul
Posts: 584
Threads: 1
Joined: Aug 2019
Apr-02-2022, 05:32 PM
(This post was last modified: Apr-02-2022, 05:32 PM by ibreeden.)
(Apr-01-2022, 10:04 PM)paul18fr Wrote: In my real case:
RealDict = {}
print(RealDict) Output: {}
None
I am sorry Paul but I do not understand you. What is the relation between this "real case" and the script you showed us?
When I run this "real case" obviously only {} is printed.
Posts: 256
Threads: 60
Joined: Apr 2019
sorry to have been not clear
The case I provided first is what I expect every time I use a dictionary, and I've used exactly the same steps so far; but in my real case (and I do not understand why), when I create an empty dictionary, both "{}" and "None" are inside leading to a crash further in the code.
Posts: 1,822
Threads: 2
Joined: Apr 2017
Please show the real code that demonstrates the problem.
Posts: 11,873
Threads: 474
Joined: Sep 2016
You cannot change the definition of the dictionary object (without consequences)
None is the known value of an empty dictionary, always.
If you need to test for that, you can check that there are values:
>>> MyDict = {}
>>> if MyDict:
... print(f"MyDict has assigned values: {MyDict}")
... else:
... print(f"MyDict is empty: {MyDict}")
...
MyDict is empty: {}
>>> MyDict['apples'] = 'Fruit'
>>> if MyDict:
... print(f"MyDict has assigned values: {MyDict}")
... else:
... print(f"MyDict is empty: {MyDict}")
...
MyDict has assigned values: {'apples': 'Fruit'}
Posts: 256
Threads: 60
Joined: Apr 2019
Thanks all for contributions and explanations.
It's mean my trouble has a different origin, and I need to find it
Posts: 1,822
Threads: 2
Joined: Apr 2017
If you're having trouble working out where the None is coming from, print out your variables at different points and check whether they are what you think they should be and whether execution is entering the branches you think. Either that, or use a debugger to step through the program as you run it.
|