General statistics
List of Youtube channels
Youtube commenter search
Distinguished comments
About
Dr Gamma D
Indently
comments
Comments by "Dr Gamma D" (@DrDeuteron) on "5 Tips To Write Better Python Functions" video.
Since you didn't ask, here are my tips for functions: docstring for users comment for devs pass arguments as keywords (for devs--from Raymond Hettinger--If you don't know who that is, you're not a pystro) single point return for successful use-cases use guard clause to catch errors early and raise/return early minimize branches (cyclic complexity). only ONE purpose per function: break it down, use private helpers if needed) so devs never scroll to see the whole function. all functions should be PURE: if it depends on state or mutates parameters, make it a class method....and don't mutate someone else's state. only 'type(self) should mutate 'self's attributes.
35
which is why you should never write a function that returns a complicated interface, unless it has a leading underbar in its name, and even then....
3
for the last one, I think I prefer the code: >>>join_text = str.join it's more pythonic, since functions are 1st class, and: don't reinvent the wheel.
3
A function should not return notimplemented or raise that associated error. That is for methods, and can be used to do some multi dispatch shenanigans, which doesn’t apply to functions
1
@sunscraper1 because functions are implemented, so not implemented doesn't mean anything.
1
which code do you find clearer: A: numbers: dict[str: int] = {'one': 1, 'two': 2} B: numbers = dict(one=1, two=2)
1
@Indently thx, but I'm secure af and only use 3.8 on the job. Higher version aren't approved so I can't check the newer features. Haven't had the time to get 3 on the to-be-replaced Mac at home, since my personal lib in 2x is huge.
1
@Indently Well I use them in 3.8, just the nested ones aren't implemented. I think they are good if you have functiond that takes a custom class, but that really only happens when two classes are cooperating (dangers of inappropriate intimacy notwithstanding), since classes have method to work with their attributes. So far, I'm 30/70 on whether to use them with primitives.
1
@replikvltyoutube3727 *a summary of PEP 8.
1