-1

Would I be able to convert these files:

### one.py ###
print("one")

### two.py ###
print("two")

to a Python class so then say I want to import one.py, I would be able to do:

importFile(MyClass.one)

or something like that?

s952163
  • 6,276
  • 4
  • 23
  • 47
C. Morgan
  • 99
  • 1
  • 10
  • 1
    You could embed code to open and import a file in a class, but to what purpose? What are you trying to do that requires that approach? – paisanco Jul 24 '16 at 23:50
  • just making a library that uses some of the stuff I made. I came across bottle.py and saw it was amazing but was only in one file, so I wanted to do the same – C. Morgan Jul 24 '16 at 23:52
  • 1
    I think your example is to simplified to correspond meaningfully to any real-word usage. If you just want one file, just make a `foo.py` file with `print("one"); print("two")` in it. No classes need to be involved in the importing, and frankly, why should they be? If you can give an example that's tied a bit more closely to reality, maybe we can answer a real question. – Blckknght Jul 25 '16 at 00:18
  • `def send_error(msg): from project.error_utils import * error_utils.send(msg)` – C. Morgan Jul 25 '16 at 00:53

1 Answers1

0

You can do this by putting those statements into a class or a method and them importing the file. This is what one.py would look like if you wanted to make it into a method and then import it:

one.py

def print_one():
    print("one")

Example file with two methods (two.py)

def method_one():
    pass

def method_two():
    print("In method 2!")

How you'd call the file (assuming you're in the same directory)

>>> import one
>>> one.print_one()
>>> import two
>>> two.method_one() # does nothing
>>> two.method_two()

You can do other more sophisticated things with Python packages by structuring your project and using things like $PYTHONPATH (documentation).

Community
  • 1
  • 1
gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
  • what if my files had defs in it? can you put classes/defs in defs? – C. Morgan Jul 25 '16 at 00:51
  • Yes. when you import the file you import all of the methods, classes, variables, etc. defined in the file with certain exceptions. – gr1zzly be4r Jul 25 '16 at 01:12
  • this helps very much, let me try it out – C. Morgan Jul 25 '16 at 02:18
  • Did this work for you? If it did you should mark the answer as correct to help others with the same question. – gr1zzly be4r Jul 25 '16 at 20:39
  • Kind of, say I use something like: `def one(): def register(): print("Registered")` and do `one() register()` register is not defined apparently – C. Morgan Jul 25 '16 at 23:22
  • You have to put something in the body of both methods for it to register. Put `pass` in the body of a method you don't want to use. – gr1zzly be4r Jul 26 '16 at 01:02
  • So `def one(): pass def register(): print("Registered")`? – C. Morgan Jul 26 '16 at 15:57
  • 1
    Understanding the code in your comments is hard because we can't tell the indentation level of each line, which makes a big difference. If you define your `register` function inside your `one` function, then the `register` function is only callable from within `one`. You can read more about namespaces [here](https://bytebaker.com/2008/07/30/python-namespaces/) – Greg Jul 27 '16 at 14:06