[Python] include block of code

In summary, the conversation discusses the process of including predefined data in a program through header files in C and how to achieve the same in Python. The expert provides the solution of using JSON for data only, and advises against using "from myHeader import *" as it can lead to namespace collisions. Instead, it is recommended to specifically import the required data or put it into a class for more organized and efficient code.
  • #1
cpscdave
403
120
In c I quite often build header files with a bunch of predefined data. Fill it with structures or what not.
Then to include that information in my program its a simple #include "myHeader.h"

Is there a way I can do this in python?
I've created another file "myHeader.py"
Have put in some code to setup these values
then in main file I put import myHeader but it doesn't appear to work as I intend.

Any suggestions??*edit* I figured I can do it by doing
from myHeader import *

SOLVED :) unless there is a more proper way to accomplish this in python :)
 
Technology news on Phys.org
  • #2
Yes you found the python way of doing it. Python doesn't have any macro features like C and hence no need to pull in a file that way like C.
 
  • Like
Likes cpscdave
  • #3
Better would be to

import myHeader as mh

then refer to the objects created in myHeader as mh.foo, mh.baz, etc. Or at least limit the names that gets pulled into your namespace using "*" by adding a line at the end of myHeader.py:

__all__ = ['foo', 'bar', 'baz']
 
  • #4
Daverz said:
Better would be to

import myHeader as mh
Or even
import myHeader

Using "from myHeader import *" is the moral equivalent of "using namespace std;" in C++.
 
  • #5
cpscdave said:
In c I quite often build header files with a bunch of predefined data. Fill it with structures or what not.
Then to include that information in my program its a simple #include "myHeader.h"
Is there a way I can do this in python?

Data only? Use JSON.

https://docs.python.org/3.4/library/json.html

edit: Ops, didn't see you were doign import *. it's a really bad idea. It imports every symbol from your module into the global namespace, which means there is a 'danger' of a collision. One day you'll add a new data field and the program will develop an error because you re-used a name. With Python, it might not crash and instead just generate subtly incorrect output. Better to specifically import what you need: 'from <x> import <y>' or put everything into a class, and import that one class instead.
 
Last edited:

Related to [Python] include block of code

1. What is a "include block of code" in Python?

An "include block of code" in Python refers to a section of code that is imported from another file or module. This is a common practice in programming to avoid repeating the same code in multiple places and to make the code more organized and modular.

2. How do I include a block of code in my Python program?

To include a block of code in your Python program, you can use the import statement followed by the name of the file or module you want to import from. For example, if you have a file named "helper_functions.py" with some useful functions, you can include it in your main program by using import helper_functions.

3. Can I include a specific function or class from a file in Python?

Yes, you can include specific functions or classes from a file in Python by using the from keyword. For example, if you only want to import a function named "calculate_average" from the "helper_functions.py" file, you can use from helper_functions import calculate_average.

4. What is the difference between importing a file and including a block of code in Python?

The main difference between importing a file and including a block of code in Python is that when you import a file, you are creating a reference to that file and its contents in your main program. On the other hand, when you include a block of code, you are directly adding that code to your program without creating a reference to the original file.

5. Are there any best practices for using include blocks of code in Python?

Yes, there are some best practices for using include blocks of code in Python. It is recommended to only include necessary code and avoid including entire files if possible. Also, it is considered good practice to use import statements at the beginning of your program to make it easier to read and understand. Additionally, you should use descriptive names for your files and modules to avoid confusion and make your code more organized.

Similar threads

  • Programming and Computer Science
Replies
1
Views
785
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
315
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
408
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top