Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to override the behaviour of the len function for a custom type?

Tags:

go

I've defined a custom type which consists of, among other things, an unexported array. What I'm wondering is, is there a way for me to override the implementation of the len() function for my custom type without having to export the array?

Thanks.

like image 275
W.K.S Avatar asked Jan 10 '14 18:01

W.K.S


People also ask

Is Len a global function in Python?

New Python users are often surprised to learn that you call a global function named len() to get the number of items in a container.

Why is len a function and not a method?

The len() built-in function deals directly with built-in types: the CPython implementation of len() actually returns the value of the ob_size field in the PyVarObject C struct that represents any variable-sized built-in object in memory. This is much faster than calling a method -- no attribute lookup needs to happen.

Why does Python use Len?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

Why is len a function?

The LEN Function[1] is categorized under Excel TEXT functions. The function will return the length of a given text string. In financial analysis, the LEN function can be useful if we wish to get the length of a given text string as the number of characters.


1 Answers

No. You can however add a Len() method to your type.

like image 50
fuz Avatar answered Oct 02 '22 14:10

fuz