Python Forum
File structure - 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: File structure (/thread-29781.html)

Pages: 1 2


File structure - macfanpl - Sep-19-2020

General question: when creating *.py files whats the order of things in file?

1. Imports, class, methods, instructions,
2. Imports, Class, additional imports

?

Is something like
x = import y
correct?


RE: File structure - buran - Sep-19-2020

PEP8 recommends imports to be at the top pf the file

PEP8 on imports

methods are parts of the class, probably you mean functions?

This
x = import y
is not valid python code and will produce error


RE: File structure - macfanpl - Sep-19-2020

(Sep-19-2020, 01:32 PM)buran Wrote: PEP8 on imports
Thanks for this. Have been reading it and one thing troubles me there.
from myclass import MyClass
From within class import itself? Or am I misunderstanding something here?


RE: File structure - buran - Sep-19-2020

myclass is module or package where MyClass class is located.

for example in module myclass(i.e. it will be file myclass.py)

class MyClass:
    # just an empty class
    pass
but as I said it well may be a package, not single module. Depends on how it's organised


RE: File structure - macfanpl - Sep-19-2020

(Sep-19-2020, 05:26 PM)buran Wrote: myclass is module or package where MyClass class is located.
Isnt this misleading?


RE: File structure - metulburr - Sep-19-2020

myclass is just whatever you name it. It is misleading if you named a module myclass. However that is a valid naming convention to lowercase the module and camelcase the class within it.

Our tutorial on modules explains in much more detail.


all imports go at the top of the file. Methods are class' function within them so to speak. And i am assuming instructions refers to instructions on how to run, or code, etc.? That would usually be commented out before the imports of the file.

'''
My 
instructions
to
do
stuff
'''

from mymodule import foo

class Klass:
    def something(self):
        pass



RE: File structure - macfanpl - Sep-20-2020

@ metulburr
Shouldnt comment line start with hash(#)?


RE: File structure - buran - Sep-20-2020

This is module docstring (i.e. also kind of comment)
Check PEP8 on comments

There is missing class on line #11 though


RE: File structure - macfanpl - Sep-20-2020

Can
import
instruction be given from within the code?
 elif 'dict' in kwargs:
            dict = kwargs.pop('dict')
            import warnings
            warnings.warn("Passing 'dict' as keyword argument is deprecated",
                          DeprecationWarning, stacklevel=2)
Is the above usage correct?


RE: File structure - buran - Sep-21-2020

the import statement can be anywhere in the code, but it will not be consistent with PEP8 recommendations. I would place import warnings at the top of the file.

That said, don't use dict as variable name - it's built-in function