Python Style Guide Questions

Discussion in 'Python' started by koranthala, Jan 16, 2009.

  1. koranthala

    koranthala Guest

    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?
     
    koranthala, Jan 16, 2009
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Jan 16, 2009
    #2
    1. Advertising

  3. koranthala

    Terry Reedy Guest

    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
     
    Terry Reedy, Jan 16, 2009
    #3
  4. koranthala

    koranthala Guest

    On Jan 16, 12:00 pm, Terry Reedy <> 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.
     
    koranthala, Jan 16, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Seberino
    Replies:
    21
    Views:
    1,768
    Stephen Horne
    Oct 27, 2003
  2. Ian Bicking
    Replies:
    2
    Views:
    1,081
    Steve Lamb
    Oct 23, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    765
    Michael Hudson
    Oct 24, 2003
  4. Maxim Khesin

    python style guide?

    Maxim Khesin, Dec 4, 2003, in forum: Python
    Replies:
    0
    Views:
    353
    Maxim Khesin
    Dec 4, 2003
  5. koranthala

    Python Style Guide Questions - Contd.

    koranthala, Jan 19, 2009, in forum: Python
    Replies:
    3
    Views:
    322
Loading...

Share This Page