General statistics
List of Youtube channels
Youtube commenter search
Distinguished comments
About
LoneTech
Tech With Tim
comments
Comments by "LoneTech" (@0LoneTech) on "Tech With Tim" channel.
Previous
1
Next
...
All
Many of the examples shown here are bad practice and not revealing what's useful about a feature. The walrus operator makes much more sense in while than if, for instance; duplicating a name is much less messy than duplicating an entire calculation in separate places like above and at the end of a block. Python has many namespaces but blocks aren't among them. Many times people are translating C-style for or while, it could be done more elegantly using iter(callable, sentinel).
2
@Czeckie Quite significantly, the right hand side of that statement doesn't matter either. This is reflected in that all([])==True, any([])==False. Both produce the initial (default) case of a reduction, i.e. all(xs) == functools.reduce(operator.and_, xs, True). all() and any() can shortcut once they find the exception.
1
Java got this from C++, but yes, the name of the easter egg could be related. Another egg contrasting other languages is "from _future_ import braces". The Zen of Python is also recorded in PEP 20.
1
That's an empty tuple inside a parenthesis. What's bizarre?
1
That's not my understanding of an l value in C, which is something assignable. The l/r is for whether an expression can be on the left (destination) side of an assignment. (x:=(y:=3)) is valid, but ((x:=y):=3) raises SyntaxError: cannot use assignment expressions with named expression. A language could choose to makee the latter valid, but Python doesn't, probably because overwriting the value you just assigned within the same expression looks like a mistake.
1
Alternative: for x in iter(y, None):
1
This is a combination of references (all names in Python are references) and mutability (values that can be altered). This is different from e.g. BASIC where variables contain values, or C++ where the two are mixed. Which behaviour is surprising depends on your background.
1
The type is a property of the value, and the value is whatever you calculated. "Duck typing" is where we work on values without needing to care if e.g. they're complex or int. Modern C++ also allows type inference by declaring variables auto. Some type level introspection is also available in both languages, i.e. type() vs typeof(). You can also declare types in Python (see typing module) and check them ahead of time using tools like pytype (which can also tell you what it infers).
1
@mv2e19 The real problem with C is the times something isn't explicit, like automatic type casts. The consequences of 1<<n vs 1ull<<n can be severe (e.g. over half the interrupt lines crashing the system). With side effects embedded into operators and function calls, ordering inside expressions can also confuse beyond the already complex operator precedence. Since those aren't native things for the computer, I think if your goal is to see how the computer operates, Scheme or Forth might be a better starting point. If your goal is to tell the computer what you want done, Haskell may be a good starting point. But if your goal is to learn one language and be able to use it everywhere, C is what's there.
1
An implementation detail is that number is usually the address in memory for that object. Implementations where addresses are unstable will have to assign the number some other way.
1
Many common immutable values are interned, i.e. prepared beforehand so we don't have to create and destroy so many objects. This includes small integers and some strings. Operations that would return them may (but don't have to) return the interned value, since immutable values can safely be shared. In CPython you can ensure a string is interned using sys.intern(). "az" and "za" are in your compiled code, while "a" is already interned in the interpreter (and does not refer to the a inside either "az" or "za").
1
The example here is not an accidental hash collision but an intentional hash identity. The numbers are equal, and so their hashes are chosen to be equal also. int, bool, float, complex and fractions.Fraction all hash integers to themselves in CPython. The purpose of the hash function is to separate objects that are definitely not equal quickly. It's not "the actual key", it is a shortcut to find the key. In general, don't mix types. Using different types of keys in the same dictionary is strange.
1
This is the sort of thing we go looking for to find out if things are unnecessarily confusing. Often it reveals a lot more about somebody's expectations. Several of these were somehow expecting equal values to not match, another was expecting to check if a boolean was greater than one. Just... why would you arrive at that in the first place? The chained operator range check is not only more useful, it's advertised. My greatest surprises were that walrus ":=" was excluded from expression statements, probably to keep assignment statements consistent, and "not" being excluded from right hand side of "==", which I imagine was done to prevent ambiguity in "is not". One could also argue that bool shouldn't be a subclass of int.
1
@stephanweinberger It's different because C's assignment operator is very error prone; leaving out one character from a comparison not only compiles but causes side effects outside the expression. The walrus operator uses the assignment operator from Pascal. The Zen of Python expresses some guiding principles of how Python is designed, for instance "errors should never pass silently". You can also look up discussions for changes, like PEP 572 in this case. I do think it's a bit odd to specially exclude the walrus operator from forming an expression statement.
1
Previous
1
Next
...
All