Liste des Groupes | Revenir à cl misc |
Lawrence D'Oliveiro <ldo@nz.invalid> writes:So why not try to write /all/ your code in a well-structured manner, so that you don't feel the need to add these "#end" comments?On Wed, 28 Aug 2024 13:29:18 -0700, Keith Thompson wrote:But ok, I found your post and removed all the #end comments. I found it>
just as readable without them as with them.
You know what? You are right. That example was just too
well-structured.
Agreed 100%.>That's not the conventional way to format a docstring. If you're using
Here’s a more dangerous one:
>
def register_additional_standard(self, **kwargs) :
"registers additional standard interfaces that are not automatically" \
" installed at Connection creation time. Currently the only one is" \
" the object-manager interface, registered with\n" \
"\n" \
" «conn».register_additional_standard(managed_objects = True)\n"
backslashes to splice lines in Python, it's likely you're doing
something wrong.
I don't know if that is true or not that it is more idiomatic - I would certainly be happy with either.for key in kwargs :I think "is not None" is more idiomatic.
if kwargs[key] :
if key == "managed_objects" :
if self._managed_objects != None :
Line continuation characters in Python are usually an indicator of poor formatting, or an unhealthy obsession with line length limits.raise asyncio.InvalidStateError \You don't need the \ if you put the ( on the same line.
(
"object manager interface already registered"
)
I personally prefer a space before the colon in Python. It is probably less common than no space, but it is certainly not a rare habit.#end ifAgain, you've decided how you want to place parentheses and you're
self.register \
(
path = "/",
interface = ManagedObjectsHandler(),
fallback = True
)
forcing the syntax to cater to that. I might write that as:
self.register(
path = "/",
interface = ManagedObjectsHandler(),
fallback = True
)
self._managed_objects = {}You leave a space between "else" and ":". It's not wrong, but it's not
else :
something I've ever seen. It's likely to be just a little jarring to
readers.
Indeed.raise TypeError("unrecognized argument keyword “%s”" % key)Do you have a requirement to use older versions of Python that don't
support f-strings?
#end ifWhy not just "return self"?
#end if
#end for
return \
self
I don't know if I am more experienced than you in Python (I may have been using Python for longer, but you may have worked with it more), but I would say that the "#end" comments directly detract from readability.#end register_additional_standardAgain, the #end comments don't make it any more readable *for me*.
>
versus
>
def register_additional_standard(self, **kwargs) :
"registers additional standard interfaces that are not automatically" \
" installed at Connection creation time. Currently the only one is" \
" the object-manager interface, registered with\n" \
"\n" \
" «conn».register_additional_standard(managed_objects = True)\n"
for key in kwargs :
if kwargs[key] :
if key == "managed_objects" :
if self._managed_objects != None :
raise asyncio.InvalidStateError \
(
"object manager interface already registered"
)
self.register \
(
path = "/",
interface = ManagedObjectsHandler(),
fallback = True
)
self._managed_objects = {}
else :
raise TypeError("unrecognized argument keyword “%s”" % key)
return self
I suspect that would be even more true for more experienced Python
programmers.
Agreed.I was looking for quite a tricky example I remember seeing on theIn any language, if a block of code is so deeply indented that it's
ArjanCodes channel on YouTube, but I can’t find it.
confusing, you should consider refactoring it. (Though that's not
always the answer.)
I once reviewed some C code with misleading indentation. Depending onAs someone who works with C programming that is often high-reliability (not necessarily safety critical, but updates after production can be very expensive), both are unacceptable in the kind of coding standards we use. I accept single-line "if" statements without braces if they are simple enough, otherwise braces are required.
the viewer's tab settings (4 vs 8 columns) it looked either like this:
if (condition)
this;
that;
or like this:
if (condition)
this;
that;
Of course it meant the same to the compiler.
In any language, I think it's important to use consistent indentationI do the same. (Well, I use tabs more than spaces, but otherwise I do the same.)
that reflects the structure of the code, and to avoid mixing tabs and
spaces. (My personal preference is to use spaces exclusively, but I'll
conform to what existing code does.) As I've said elsethread, Python's
rules just force me to do what I would have done anyway.
Les messages affichés proviennent d'usenet.