Python Forum
Idiomatic Python compared
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Idiomatic Python compared
#1
I've been using python a lot after many years on Ruby. I've been indoctrinated on Ruby community opinions about code smells and idiomatic programing. I bring that perspective to writing pythonic code. And I find a few differences that I want to ask about.

In the ruby world we are strongly discouraged from writing long functions or methods. More than 20 or so lines is a code smell. I agree with that because I think a bias towards short methods leads to simpler and easier to understand code. Both in reading about idiomatic python (pythonic) and reading other people's code, I find no such recommendation or bias. Is that correct?

In the ruby world we are encouraged to try to have source code files that have code that is very related. In python I find no such recommendation, and in fact have found much source code with very long source files where the code is not very strongly related, it's kind of everything. Is that correct?

I have looked at a bunch of python style guides. Can someone help give me some more context?
Reply
#2
Python does not force you to keep functions (or class methods) simple, but it is certainly desired.
here's the 'guide': https://www.python.org/dev/peps/pep-0020/

or from command line:
$ python
>>>import this
Reply
#3
Yes, I guess obviously it doesn't force you. But is it a strongly held sense of the pythonic community that long methods or functions are a *code smell* ?
Reply
#4
(Sep-28-2021, 02:00 PM)pitosalas Wrote: But is it a strongly held sense of the pythonic community that long methods or functions are a *code smell* ?
Yes,in general should avoid long function/method.
Do one thing well in a function/method is usually a good advice.
Many of us have pointed this and given refactoring advice numerous of times,
when people here have posted like 30+ up 300 lines in one function.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020