Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Style Guide Questions

Reply
Thread Tools

Python Style Guide Questions

 
 
koranthala
Guest
Posts: n/a
 
      01-16-2009
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?
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-16-2009
On Thu, 15 Jan 2009 22:13:06 -0800, koranthala wrote:

> 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.


--
Steven
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-16-2009
koranthala wrote:
> 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

 
Reply With Quote
 
koranthala
Guest
Posts: n/a
 
      01-16-2009
On Jan 16, 12:00*pm, Terry Reedy <(E-Mail Removed)> wrote:
> koranthala wrote:
> > 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


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Style Guide Questions - Contd. koranthala Python 3 01-23-2009 03:23 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM



Advertisments