Error: Object of type 'float' has on Len

Asked

Viewed 1,833 times

7

I was thrashing simple operations on the Python interpreter, and the following command made me curious:

>>> b = 3.12; len(b)

With the following error:

Traceback (Most recent call last):

File "", line 1, in

Typeerror: Object of type 'float' has no Len()

In my understanding the interpreter Python is accusing that the object of type float has no size. I don’t know, I was hoping that the output would be 3 or 4 which would be the amount of digits it shows, counting the point or not.

2 answers

5

It even has a size in memory, but the function len() is not to analyze the size occupied in memory. And it’s not even the intention, after all its expectation is that it shows the amount of digits that is shown in the number. They are completely different things. The way the data is in memory and how it’s shown on the screen - or is stored somewhere for a human to see - are very different things.

You’re seeing a representation of the data in the form of graphic characters that happen to be numerical digits. But the important thing here is that they are characters. What you are seeing is a string (string) of characters, that is, you are seeing a string. So to know the size of this string, you have to say you want this. You have to turn the number into the type string and then you can use the function len() to know how many characters she has.

The function len() is available for some data types, not for all. The numeric data itself has a size in memory that is an implementation detail. We know it’s 4 bytes, but that doesn’t matter most of the time. Len is the contraction of length, that is, length. So it doesn’t exactly provide the size of anything, but rather the length. There’s a subtle difference there. The length can be obtained in any type that is a sequence of something. That is the case of a string.

The simplest way to convert float for string is with the function str():

b = 3.12; len(str(b))

Another way is with the repr():

b = 3.12; len(repr(b))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • really @bigown in python documentation does not specify the float type in the Len() function but Voce would know how I can then recover the amount of characters from a float variable, another variable question of type int and of type float and others of type numeric occupy the same space in memory is that 4 bytes

  • 1

    The way I showed you above. I put an example working now to see. In Python both the int, as to the float occupy 4 bytes, but I do not know if this is a guarantee of the language (I believe it does), have languages that guarantee this, others leave open and can not trust this.

  • The "Len" function does not display the "number of digits" of a number nor the number of characters of a string - it returns the length of a string. The size in Bytes in the memory of a Python object has nothing to do with whether or not to implement the sequence protocol (and therefore whether or not to have`Len')

  • And that’s just what I said. When you don’t understand the answer, you ask for clarification, not negative.

  • I understood the answer - the negative is because of "it seems to me that neither is the intention". It is not the intention even. And it has nothing to do with the number of digits - it has to do with whether the object is a sequence or not: are concepts wrong enough for me to deny the answer - wow since they are misleading.

  • But I thought that the OP wanted to know the size of the float in memory, in fact, it didn’t even occur to me that he might be wanting, in fact, the length of the decimal representation of the data - in this sense, in fact, the answer doesn’t need to be negative. But the O.R. blocks me from changing my vote unless the answer is edited.

  • 1

    His comment makes it clear that’s exactly what he wants.

  • ok. I assumed that he wanted the size on the breast of a float, just because I read the beginning of his answer. For me to take the negative vote, it needs to be edited - you could change only the "It seems to me that’s not even the intention" to "Nor is that the intention"?

  • 1

    Okay, I made it clear.

  • my question was kind of without foot and nor cacbeça since the result I was hoping to get was the length of my variable float and had nothing to do with memory

  • @Sandrog we understand, when one is learning the concepts get confused, but I’m sure now understand better the subject.

Show 6 more comments

4

Unlike C, the size that Python objects occupy in memory is abstracted, and at first a Python program does not need to know the same.

Of course it is good to have a notion - and eventually you can optimize something, for example, using namedtuples instead of dictionaries, etc.... The function len is practically a Python operator: it returns the number of items in a sequence - and this has nothing to do with the size of the sequence object in memory, nor with the objects within the sequence.

An integer, or float, or other type is not a sequence. A text string is a sequence, but the value returned by len for it may have no relation to its memory size: if it is a Unicode text object, each character can occupy 4 bytes. If it is a text encoded in utf-8, the characters have variable length in bytes, and so on.

That said, Python has an auxiliary function to tell the size in bytes of an object in memory - in the module sys, the function getsizeof.

Then you’ll get what you were trying to do:

>>> import sys
>>> b = 3.12
>>> sys.getsizeof(b)
24

And the answer is this: 24 - a "float" Python object occupies 24 bytes in memory. The data itself, the number, in the specific case of the Python float is a 64bit IEEE 754 float - which uses 8 bytes - the remaining 16 bytes of the float object are metadata used by Python to control the life cycle of the object, among other things.

As I said before, this information is not very useful. Or your program will have a few floating point variables - less than 10 or 20, and do some accounting, and in this case that size in bytes on a machine with several hundred megabytes of memory, as is typically the memory of machines running Python, is irrelevant, or, if you are working with scientific computing, in which the memory size of a number can make a difference, you will be using specialized objects to store its numbers (even though it is essential that the mathematical and logical operations over mass data be done in native code). Normally numpy library arrays are used in code of this type - but you can also use Python stdlib native arrays. Either of the two allows you to specify the exact data type of each element, and there you can choose to use 32 or 64bit floats (4 or 8 bytes per number). In the case of numpy there is even support for 128bit floats (16 bytes, but this can be slow, since there is no hardware support on current Cpus for this type of data).

  • now managed to give a lightening, need to know more the functions of python and thanks for the help vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.