Python Forum
Python-Function object creation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python-Function object creation (/thread-4182.html)



Python-Function object creation - studnik2 - Jul-28-2017

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.


RE: Python-Function object creation - Larz60+ - Jul-28-2017

If you're not going to call the function doesn't it make sense not to write it?


RE: Python-Function object creation - DeaD_EyE - Jul-28-2017

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.