pythonic code_Pythonic code style [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 5 years ago.

I am an amateur programmer writing some python for a research project at my university. I need my code to be very readable for anyone who may be working on this project after me, and as such I am trying to follow PEP 8. I have run into a conflict of rules however. The offending lines are a dictionary definition after a long string of for loops and if statements. The problem is that convention has it that no line should be longer than 79 characters, yet line continuations should be indented from where they started from. I see three options of what could logically be done, but am unsure of what is best.

option 1: leave offending lines too long

defgetIndexedData(directory):|...|...|...|# construct dictionary of images with peak locations |peaks[image]={|'Xpixel':[float(x)forxinstep[17][10][0].text.s|plit(' ')],'Ypixel':[float(x)forxinstep[17][10][1].text.s|plit(' ')]}returnpeaks|

option 2: unindent continued lines

defgetIndexedData(directory):|...|...|...|# construct dictionary of images with peak locations |peaks[image]={|'Xpixel':[float(x)forxinstep[17][10][0].text.split(' ')],|'Ypixel':[float(x)forxinstep[17][10][1].text.split(' ')]}|returnpeaks|

option 3: split definitions somewhere (not sure where)

defgetIndexedData(directory):|...|...|...|# construct dictionary of images with peak locations |peaks[image]={|'Xpixel':|[float(x)forxinstep[17][10][0].text.split(' ')],|'Ypixel':|[float(x)forxinstep[17][10][1].text.split(' ')]}|returnpeaks|

I am also open to any other suggestions :)

Thanks,

~Aaron

解决方案

A couple of relevent snippets from PEP 8 with my italics added.

A style guide is about consistency. Consistency with this style guide

is important. Consistency within a project is more important.

Consistency within one module or function is most important.

But most importantly: know when to be inconsistent -- sometimes the

style guide just doesn't apply. When in doubt, use your best judgment.

Look at other examples and decide what looks best. And don't hesitate

to ask!

... it is okay to increase the nominal line length from 80 to 100

characters (effectively increasing the maximum length to 99

characters), provided that comments and docstrings are still wrapped

at 72 characters.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值