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?
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
(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?
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
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
@ metulburr
Shouldnt comment line start with hash(#)?
This is module docstring (i.e. also kind of comment)
Check
PEP8 on comments
There is missing
class
on line #11 though
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?
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