Is it part of clean code to include docstrings in Python? [closed] - python

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 7 years ago.
Improve this question
I am working on a project, and I would like to have my code as clean as I can. I am wondering if including docstrings in a class it is considered as clean code or not. For example, one of my class definitions is as follows:
class Hotel:
"""Class to model the rating and rates of a hotel."""
def __init__(self, rating):
"""
:type rating: int
:param rating: hotel rating
"""
self.rating = rating
Is it correct the way I am writing the code, i.e., am I following the clean code standards.
Thanks in advance.

Clean code is partly a matter of opinion. But most of the ones I'm aware of for Python favor including class and method docstrings. For instance, the Google Python Style Guide says:
A function must have a docstring, unless it meets all of the following
criteria:
not externally visible
very short
obvious
...
That said, there's more to good docstrings than just having them; a good place to start is by asking yourself, what would I need to use this class (or function) if I had never seen it before?

Related

Is calling a function within a function signature an anti-pattern? [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 2 years ago.
Improve this question
This is not a problem I've encountered, but rather just a curiosity about best practices/style in python:
I see stuff like this quite often:
class A:
def __init__(self, qux):
"""...do whatever... maybe this is expensive - maybe it isn't - maybe I don't know if it's expensive"""
def get_baz(foo, bar=A(2)):
"""...do another thing..."""
return baz
I largely avoid function/method calls (like bar=A(2)) in function signatures, because this makes A.__init__ run on import. Do you think avoiding the running of code on import is best practice, or am I just OCD and it's totally fine to handle this on a case-by-case basis? I feel like there is an expectation (at least in the python community) that importing a package/module shouldn't take a significant amount of time/memory.
The common alternative to the above is slightly more verbose, but runs no extra code on import:
def get_baz(foo, bar=None):
if bar is None:
foo = A()
Which do you like more and why? Should I just not care?
(For context, I started thinking about this because importing an internal package at my company took 8 seconds, which was triggering.)

can someone explain the purpose of the strategy metaclass mentioned here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Cannot link the example to the title, so here it is: Python strategy design pattern example
First let's look at the basic idea of Strategy Pattern. What it really says is developing some algorithm (function/method/code fragment) which can be switched at run time.
If we are using an OOP supported language (Java, Python), most of the time, we can implement the Strategy pattern with the use of run time polymorphism. In your example also it shows how to achieve this exactly in that way.
No need to get confused with the notion of meta class. It's a python specific terminology, which is used to define the class object of Python. This one is a good answer, if you want to know more about meta classes in python.
And in your example, the notion of meta class doesn't quite related with Strategy pattern implementation. It was just used to mark the Strategy class there as an abstract class. You can develop your program even without that part. And it doesn't do any harm to the idea of Strategy pattern implementation.

Python attribute resolution: the ultimate flowchart [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a complete description of Python attribute lookup. I know there have been similar questions and there is this very nice introduction to the descriptor protocol. But it does not cover the entire lookup for example it does not elaborate on special methods.
Ideally, there would be a flowchart starting at x.y and then running through all possibilites including
is it lookup or assignment?
is x a class or an object (or for example a function if that makes a difference)?
is x a builtin if that's relevant?
does x have a __slots__ attribute?
does x have a __getattr__, __setattr__ or __getattribute__ method?
is y, the string a special attribute name
once y is resolved, does it have a __get__ attribute?
etc., etc., you get the idea.
I realise this is a lot of work, so if there are any promising submissions within 5 days from now I'm willing to offer a 250 rep bounty.
Reference version should be Python3.6 and there should be an effort made to demonstrate completeness as far as possible.
I'm sure such a flowchart would be tremendously useful for me and many others.

Documenting ctypes fields [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 4 years ago.
Improve this question
I am working on a python module that is a convenience wrapper for a c library. A python Simulation class is simply a ctypes structure with a few additional helper functions. Most parameters in Simulation can just be set using the _fields_ variable. I'm wondering how to document these properly. Should I just add it to the Simulation docstring? Or should I write getter/setter methods just so I can document the variables?
When I do similar things, if it is a small class I will put everything in the same class, but if it is bigger, I typically make a class that only contains the fields, and then a subclass of that with functions. Then you can have a docstring for your fields class and a separate docstring for your simulation functions.
YMMV, but I would never consider adding getters and setters for the sole purpose of making the documentation conform to some real or imaginary ideal.

is the common practice in python to put __init__ in the beginning of class definition? [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 8 years ago.
Improve this question
I wonder whether the recommended practice is to put init in the beginning of class definition or to put interface functions first:
class examp1:
def ifmethod1(self):
pass
def ifmethod2(self):
pass
def __init__(self):
pass
VS.
class examp1:
def __init__(self):
pass
def ifmethod1(self):
pass
def ifmethod2(self):
pass
A lot of people put it first, but every one can have their own opinion and you'll find arguments for both.
To me, being first is simply useful because it's helpful to know __init__ quickly.
In an effort to help coders of other languages grasp Python quickly, ibiblio.org says "__init__ is analogous to a constructor". In that spirit, it would often be put first — constructors in other languages are almost always listed first.
In one example, the Google style guide for Python, it is always listed first.

Categories