ugly code? #18163
Replies: 6 comments 3 replies
-
|
The pros minimise the use of globals. It is usually best to construct classes and to use instance variables where data must be shared between methods. Where a function or method fails, consider raising an exception rather than returning I suggest you find a book or online course on object oriented programming in Python. Perhaps someone will suggest a good one. |
Beta Was this translation helpful? Give feedback.
-
|
There is no ugly code, IMHO. It either works or it doesn't, and "working" has a wider meaning depending on the context: readable, maintainable, etc. Yes, Python has an implicit I tend to use explicit namespaces instead of class K(): pass
NS = (K(),K()) # namespaces
def func(i):
if i == 0: x = 0; y = 1
else: x = 1; y = 0
def f(n):
try:
NS[x].n += n
print(f'NS({x}) {NS[x].n}, {NS[y].n}, {NS[x].n+NS[y].n}')
except:
NS[x].n = n
return f
NS[0].proc = func(0)
NS[1].proc = func(1)
del func # now, only 'f' visible in namespaces as 'proc'
NS[0].proc(2)
NS[1].proc(2)
#
NS[0].proc(2)
NS[1].proc(2)I like this pattern because: 1) loosely-couple, 2) message passing. But, there will be dragons. Perhaps others will consider this as an anti-pattern. |
Beta Was this translation helpful? Give feedback.
-
r=(lambda _, __: list((___[1] for ___ in __(_))))([[567, 345, 234], [253, 465, 756, 2345], [333, 777, 111, 555]], lambda _: (sorted(_) for _ in _)) |
Beta Was this translation helpful? Give feedback.
-
|
I find that using a static type checker such as an IDE integrated or standalone pyright or mypy very effectively helps to spot these None type errors even before running the code. For MicroPython just add the MicroPython-stubs |
Beta Was this translation helpful? Give feedback.
-
|
Haven't had any success installing the likes of VScode Jos but something that can spot potential trouble in advance sounds worthwhile. Something functional that gets past the load stage would be nice. |
Beta Was this translation helpful? Give feedback.
-
|
I'm on windows 11 & alternate between Atom & Thonny |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I like writing code. I'm not particularly good at it but refining a program progressively is satisfying. I liken it to rolling a turd in glitter.
At the start of my upython experience I got a bit wary of the return statement. My main.py code would crash with TypeError: 'NoneType' object isn't iterable when fns due to return a bunch of variables had a problem & returned None instead, 'if a_fn() is not None' stmts started to sprout like weeds.
Eventually I got into the habit of putting anything global in a dict & operating on them directly in the functions, return stmt not required. This has worked OK but gives me clumsy variable names like glbl['batt']. On the upside having multiple variables all in a single dict makes it easy to save them in rtc memory before a deepsleep.
Just wondering if I'm using a screwdriver as a can opener & what other options there might be?
Beta Was this translation helpful? Give feedback.
All reactions