Re:Python’s tug of war between beginner-friendly and advance...
by LostJourneyman on Friday February 05, 2021 @08:23PM
(#26043259)
Quick note, the more pythonic way of checking par in this case would be just to:
if par:
do_stuff()
Python does some really cool, if slightly unintuitive, checks for "truthiness" where 0, [], {}, false, '' and None (could be other cases too, brain's a little foggy today) all evaluate as false. This controls for: if par = None:
do_stuff()
not catching empty lists, empty dicts, etc.
Re:Python’s tug of war between beginner-friendly and advance...
by andi999 on Saturday February 06, 2021 @09:04AM
(#26046534)
How do you define the function in that case?
Re:Python’s tug of war between beginner-friendly and advance...
by LostJourneyman on Sunday February 07, 2021 @10:46AM
(#26055750)
So, a more complete example might help:
def myFunc(par=None):
if par:
# do whatever is needed when the parameter exists
pass
#finish processing
This allows you to pass in [], {}, None, '', or omit par all together, which will all behave as if par=None, or pass in data and use it appropriately. You can also use if not par:
pass
if you want to do the inverse.
Re:Python’s tug of war between beginner-friendly and advance...
by vasachi on Friday February 05, 2021 @01:25AM
(#26033668)
I believe the idiom to check, if a parameter wasn’t set is to use an object() instance as default value.
_notset = object()
def foo(x=_notset):
if x is _notset:
print("foo was called as foo()")
Re:Python’s tug of war between beginner-friendly and advance...
by andi999 on Friday February 05, 2021 @06:29AM
(#26035198)
Thats nice. Thanks. Where did you find it.
Re:Python’s tug of war between beginner-friendly and advance...
by vasachi on Saturday February 06, 2021 @06:36AM
(#26045885)
I think it is from somewhere in standard library.
Re:Python’s tug of war between beginner-friendly and advance...
by maxs on Friday February 05, 2021 @02:50AM
(#26034070)
I don't understand what you mean by arrays.
if par is None: ...
As for accessing dicts with a struct syntax, I prefer keeping dicts and objects different things (unlike Javascript).And for JIT, there's numba!
Agreed on the switch statement...
Also what is wrong in having a switch statement? (I`d like one with range cases 1<x<10..)
Accesing dicts with a struct syntax.
'Inline' C functions.
JIT, especially for loop acceleration.