Python Forum

Full Version: Python-Function object creation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
As I understand from a book that function in Python is nothing but object of Function class. I have some doubts as below:

1.When this object gets created? At the time we define function or at the time we call a function?

2.If it is getting created at the time we define a function, then will it not be a waste of memory if we do not call that function anywhere in program ?

Looking for detail answer.
If you're not going to call the function doesn't it make sense not to write it?
Quote:1.When this object gets created? At the time we define function or at the time we call a function?
The Python interpreter is parsing the whole code from your script or stdin and convert it to bytecode, which runs in the interpreter.

Quote:2.If it is getting created at the time we define a function, then will it not be a waste of memory if we do not call that function anywhere in program ?
The function object lives in memory at runtime. Yes, if you define it as wasting memory, it is wasting memory. But if you aware of memory you should better use C or assembler.