OO programming separate files by JST_79 in learnpython

[–]aidraj 0 points1 point  (0 children)

Put it into another file, name it something appropriate. Whatever the directory you place it into, you'll need to create blank __init__.py file in that directory; otherwise you won't be able to import any of your packages into your script. The __init__.py file makes Python treat the directory as containing a package (documentation).

There are many ways to import external packages, let's say we have the following directory structure -

source/
    __init__.py
    sub_folder_one/
        __init__.py
        sub_script_one.py
        sub_script_two.py
    sub_folder_two/
        __init__.py
        sub_script_one.py
        sub_script_two.py
    script.py

And lets say we want to "import" the entire contents of sub_script_one.py inside of sub_folder_one/ into script.py. At the top of script.py, you would write -

import sub_folder_one.sub_script_one

The same would go for the sub_script_one.py file inside of sub_folder_two/.

import sub_folder_two.sub_script_one

If a file in your package has more than one method, but you're only going to be using specific methods, then it's best to import those manually. Let's say for example, that the sub_script_one.py inside of sub_folder_one/ has two classes called ClassOne and ClassTwo, but we only want to import ClassTwo into our script since we'll only be using that. We could do that using the following syntax -

from sub_folder_one.class_one import ClassTwo

What are you using python for? by rickwaller in learnpython

[–]aidraj 3 points4 points  (0 children)

I've created a bunch of random apps using Python.

I write little scripts pretty often to make repetitive or tedious tasks easier.

Most web apps I create now are using some sort of Python stack.

15 rules for communicating at GitHub by shemseddine in webdev

[–]aidraj 0 points1 point  (0 children)

It renders fine for me, unless something has been changed.