{"id":24562,"date":"2021-10-21T10:07:15","date_gmt":"2021-10-21T04:37:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=24562"},"modified":"2021-11-05T19:38:07","modified_gmt":"2021-11-05T14:08:07","slug":"python-compile-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-compile-function-with-examples\/","title":{"rendered":"Python compile() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for bytes() Function<\/a> The compile() function returns the specified source as a ready-to-execute code object.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n source:<\/strong> This is Required. A String, a Bytes object, or an AST object can be used as the source to compile.<\/p>\n filename:<\/strong> Required. The name of the file from which the source was obtained. If the source is not a file, you can type whatever you want.<\/p>\n mode:<\/strong> This is Required. Legal principles:<\/p>\n flag:<\/strong> This is Optional. The process of compiling the source code. 0 is the default.<\/p>\n dont_inherit:<\/strong> This is Optional. The process of compiling the source code. False by default<\/p>\n optimize: <\/strong>This is\u00a0Optional. Defines the compiler’s optimization level. -1 is the default.<\/p>\n Return value:<\/strong><\/p>\n Python code object is returned by the compile() function.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n In this case, the source is a regular string. multstring is the filename. Furthermore, the exec mode later allows the use of the exec() method.<\/p>\n The compile() method transforms a string into a Python code object. The exec() method is then used to execute the code object.<\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for bytes() Function compile() Function in Python: The compile() function returns the specified source as a ready-to-execute code object. Syntax: compile(source,\u00a0filename,\u00a0mode,\u00a0flag,\u00a0dont_inherit,\u00a0optimize) Parameters: source: This is Required. A String, a Bytes object, or an AST object can be used as the source to compile. filename: Required. The …<\/p>\n
\ncompile() Function in Python:<\/strong><\/p>\ncompile(source,\u00a0filename,\u00a0mode,\u00a0flag,\u00a0dont_inherit,\u00a0optimize)<\/pre>\n
\n
Given code in string = 'p = 4\\nq=7\\nmult=p*q\\nprint(\"The multiplication result =\",mult)'<\/pre>\n
The multiplication result = 28<\/pre>\n
Given code in string = 'p = 25\\nq=5\\ndivisn=p\/q\\nprint(\"The division result =\",divisn)'<\/pre>\n
The division result = 5.0<\/pre>\n
Program for compile() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the Python code as a string that multiplies the given two numbers\r\n# and store it in a variable.\r\ncodeIn_gvnstring = 'p = 4\\nq=7\\nmult=p*q\\nprint(\"The multiplication result =\",mult)'\r\n# Compile the given code using the compile() function with the parameters\r\n# as the above string code, codename, and mode of compilation.\r\ncode_obejct = compile(codeIn_gvnstring, 'multstring', 'exec')\r\n# Print the above result which is the code after compilation.\r\nexec(code_obejct)\r\n<\/pre>\n
The multiplication result = 28<\/pre>\n
\n