Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Fix “TypeError: len() of unsized object”

#1
How to Fix “TypeError: len() of unsized object”

How to Fix “TypeError: len() of unsized object”

Problem Formulation: How to fix the TypeError: len() of unsized object?

TypeError: len() of unsized object

There are many possible ways why this array may occur. One common pitfall is to use the len() function on a NumPy array with only one value.

Example: Let’s consider the minimal example that creates this error message!

>>> import numpy as np
>>> a = np.array(5)
>>> a
array(5)
>>> len(a)
Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> len(a)
TypeError: len() of unsized object
>>>

Reason why this fails: The array a consists of only one value 5. The fact that the array consists of only one value makes the NumPy array a a scalar—not a container type on which you can call the len() function. You cannot use the len() function with a scalar because scalars are not container types that can have 0 or more elements. Scalars always consist of one element and the len() function is not defined on them!

>>> len(42)
Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> len(42)
TypeError: object of type 'int' has no len()

Solution: Instead of relying on the len() function to determine the number of elements in a NumPy array, use the array.size property that is always defined whether it’s a scalar array or not.

Here’s the same example without the TypeError: len() of unsized object:

>>> import numpy as np
>>> a = np.array(5)
>>> a
array(5)
>>> a.size
1

Note: The size property only works for NumPy arrays, not for other types of scalars such as integers or floats. For those, it really doesn’t make any sense to run the len() function on.

Thanks for reading this article, I hope it saved you some time! ?

The post How to Fix “TypeError: len() of unsized object” first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...ed-object/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python object() Function xSicKxBot 0 30 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] How to Check if an Object is of Type List in Python? xSicKxBot 0 27 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  [Tut] How to Get The Current Reference Count of an Object in Python? xSicKxBot 0 44 12-21-2020, 08:13 AM
Last Post: xSicKxBot
  [Tut] Python TypeError: Object is Not Subscriptable (How to Fix This Stupid Bug) xSicKxBot 0 65 11-13-2020, 12:04 AM
Last Post: xSicKxBot
  [Tut] How to Solve Python “TypeError: ‘int’ object is not iterable”? xSicKxBot 0 124 09-18-2020, 01:49 PM
Last Post: xSicKxBot
  [Tut] How to Access an Object Attribute Given the Attribute Name as a String? xSicKxBot 0 90 09-04-2020, 08:14 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) FREE CMC: Ice Crusade, 80% ...
Last Post: xSicKxBot
Today 03:46 AM
» Replies: 0
» Views: 0
(Free Game Key) Foglight Online - Free o...
Last Post: xSicKxBot
Today 03:46 AM
» Replies: 0
» Views: 0
AppleInsider - Jailbreak tool ‘unc0ver’ ...
Last Post: xSicKxBot
Today 03:45 AM
» Replies: 0
» Views: 0
News - Beta: Bedrock 1.16.210.59/60/61
Last Post: xSicKxBot
Today 03:45 AM
» Replies: 0
» Views: 0
News - Panzer Dragoon II Zwei: Remake Ta...
Last Post: xSicKxBot
Today 03:45 AM
» Replies: 0
» Views: 0
Xbox Wire - Call of Duty: Black Ops Cold...
Last Post: xSicKxBot
Today 03:44 AM
» Replies: 0
» Views: 1
News - Hiro Capital leads $15 million in...
Last Post: xSicKxBot
Today 03:44 AM
» Replies: 0
» Views: 0
News - Get a job: Join Insomniac Games a...
Last Post: xSicKxBot
Today 03:44 AM
» Replies: 0
» Views: 0
News - Feature: Zelda II: The Adventure ...
Last Post: xSicKxBot
Yesterday 09:11 PM
» Replies: 0
» Views: 2
[Tut] How To Extract All Emojis From Tex...
Last Post: xSicKxBot
Yesterday 01:58 PM
» Replies: 0
» Views: 2

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020