Python Style Guide Questions

K

koranthala

Hi,
Which is more advisable?
import x
b = x.a
or
from x import a
b = a

I read in Learning Python that it is always better to use the
former - especially since namespace wont be dirtied. But, doing that
with PEP 8 together causes my code to look rather cluttered. Reason
being that - PEP 8 suggests that have max line length = 79 chars.
So my code mostly looks like this:
class x:
def y():
try:
if test:
obj.filename = str(os.path.basename
(obj1.find_next_element().\
get_file_path()))
obj.modify_time = obj.filename.find_created_time()
+ \
datetime.timedelta
(seconds=time.find_time())

etc .. etc..
Almost every line requires the '\'. Also, especially since Python also
uses whitespace as indentation, I keep confusing the block indentation
with the indentation that the '\' causes in the next line.

Could you please let me know what you guys usually do in such cases?
Is it advisable to go for the
from x import a to avoid this clutter?
 
S

Steven D'Aprano

Hi,
Which is more advisable?
import x
b = x.a
or
from x import a
b = a

I read in Learning Python that it is always better to use the
former

Perhaps not "always", but often.
- especially since namespace wont be dirtied. But, doing that
with PEP 8 together causes my code to look rather cluttered. Reason
being that - PEP 8 suggests that have max line length = 79 chars. So my
code mostly looks like this:
class x:
def y():
try:
if test:
obj.filename = str(os.path.basename
(obj1.find_next_element().\
get_file_path()))
obj.modify_time = obj.filename.find_created_time()
+ \
datetime.timedelta
(seconds=time.find_time())


A few ideas for you:


There is rarely enough performance benefit from squashing as much as
possible into a single expression to make up for the loss of readability.
Use temporary values to aid comprehension and readability.


class X:
def y(self):
try:
if test:
path = obj1.find_next_element().get_file_path()
obj.filename = str(os.path.basename(path))
ctime = obj.filename.find_created_time()
offset = datetime.timedelta(seconds=time.find_time())
obj.modify_time = ctime + offset


Alternatively, don't nest so much.

class X:
def _y(self):
path = obj1.find_next_element().get_file_path()
obj.filename = str(os.path.basename(path))
ctime = obj.filename.find_created_time()
offset = datetime.timedelta(seconds=time.find_time())
obj.modify_time = ctime + offset
def y(self):
try:
if test:
self._y()


Last but not least, Python now does automatic line continuations inside
open brackets. You can use this to eliminate many backslashes.

class X:
def y(self):
try:
if test:
obj.filename = str(
os.path.basename(
obj1.find_next_element().get_file_path()
))
obj.modify_time = \
obj.filename.find_created_time() + \
datetime.timedelta(seconds=time.find_time())



Hope this helps.
 
T

Terry Reedy

koranthala said:
Hi,
Which is more advisable?
import x
b = x.a
or
from x import a
b = a

If I know I want just one thing from x, I tend to use latter.
I also like 'import xyzlib as x'
I read in Learning Python that it is always better to use the
former - especially since namespace wont be dirtied.

Namespace get cluttered, not dirtied. In any case, either 'x' or 'a'
gets added. One new name either way.
class x:
def y():
try:
if test:
obj.filename = str(os.path.basename
(obj1.find_next_element().\
get_file_path()))
obj.modify_time = obj.filename.find_created_time()
+ \
datetime.timedelta
(seconds=time.find_time())

Use 4 spaces instead of 5 for indents
Almost every line requires the '\'.

As Steven said, usually no
Also, especially since Python also
uses whitespace as indentation, I keep confusing the block indentation
with the indentation that the '\' causes in the next line.

'\' causes no indentation

Beyond that, consider find a style you like. I agree with Stvhen about
using 'thought-sized' expressions.

tjr
 
K

koranthala

If I know I want just one thing from x, I tend to use latter.
I also like 'import xyzlib as x'


Namespace get cluttered, not dirtied.  In any case, either 'x' or 'a'
gets added.  One new name either way.


Use 4 spaces instead of 5 for indents


As Steven said, usually no


'\' causes no indentation

Beyond that, consider find a style you like.  I agree with Stvhen about
using 'thought-sized' expressions.

tjr

Thank you Steven and TJR. I will try to implement the thought sized
expressions.
I was rather misled by trying to decrease the line count as much as
possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top