Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File structure
#1
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?
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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?
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Sep-19-2020, 05:26 PM)buran Wrote: myclass is module or package where MyClass class is located.
Isnt this misleading?
Reply
#6
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
Recommended Tutorials:
Reply
#7
@ metulburr
Shouldnt comment line start with hash(#)?
Reply
#8
This is module docstring (i.e. also kind of comment)
Check PEP8 on comments

There is missing class on line #11 though
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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?
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard disk structure like a file selection dialog malonn 2 788 Aug-09-2023, 09:14 PM
Last Post: malonn
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,703 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  File system representation in a data structure Alfalfa 1 2,057 Dec-18-2019, 01:56 AM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020