getting memory usage of varaibles

Ben Finney ben+python at benfinney.id.au
Thu May 4 00:18:03 EDT 2017


Erik <python at lucidity.plus.com> writes:

> The thing about functions or classes is that you can't (at the literal
> source level) define them *without* giving them a name:

Even a function is commonly defined without giving it a name.

    >>> strategies = [
    ...     (lambda x: x + 2),
    ...     (lambda x: x ** 3),
    ...     (lambda x: None),
    ...     ]
    >>> type(strategies[0])
    <class 'function'>

The objects in that list exist, they are functions, and there is no name
referencing any of them.

So yes, it's normal to define a function without giving it a name. (And
it is also normal to define a function with a name.)

(I'm not aware of anything equivalent for class definition; certainly
nothing as commonly used as ‘lambda’ for defining a function. And maybe
that's a good thing.)

> But lists and tuples and ints and strings and dicts and sets and
> floats and probably something else I've forgotten can be spelled as
> anonymous literals.

And that includes functions, too.

The idea of a “variable” dies hard, it seems. Objects don't always need
names, and Python programmers just have to learn to live with that.

-- 
 \              “Ignorance more frequently begets confidence than does |
  `\           knowledge.” —Charles Darwin, _The Descent of Man_, 1871 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list