« first day (4233 days earlier)      last day (708 days later) » 
00:00 - 21:0021:00 - 23:00

overwrite is not a word I would have considered using. I think I'll use that question as a "see also", and let it stay open.
This is partly because the specific case of "remove stuff from a binary tree" is a common prompt for students to ask questions like this one. In the long term, the question could be edited (especially the title) to be useful in that regard.
(Graph structures, such as binary trees, need special treatment for this topic: an answer like stackoverflow.com/questions/18896099/… won't help, because just doing return None doesn't modify the structure. The key insight is that it's actually the parent node that changes.)
 
9:21 PM
Hello. Is there a nicer way writing this (as nullish coalescing doesn't exist)?
if isinstance(variable) and variable.function():
 
isinstance(variable, what?)
 
What do you mean?
I need to check if the variable is set before running the .function()
 
>>> isinstance(42)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [389], in <cell line: 1>()
----> 1 isinstance(42)

TypeError: isinstance expected 2 arguments, got 1
 
Oh, right
 
What does "variable is set" mean?
 
9:23 PM
But alright, let me be more clear
 
Variables are always "set". Do you mean you have to check if it's not None?
 
if variable and variable.function_that_returns_boolean():
    do_this()
 
What is your expected failure mode for that first test? What is the value of variable?
 
if (variable?.function_that_returns_boolean())
{

}

works in javascript
Well, the variable has this signature
 
9:25 PM
then use javascript
 
TIL javascript has an elvis operator
 
variable: Union[subprocess.Popen, None]
So it can be either None or the specific type
 
@Warcaith that wasn't so hard, was it?
 
(or yeah, should be)
Huh?
I was wondering if there was a cleaner way than:


if variable and variable.function_that_returns_boolean():
 
if variable is not None would be more explicit, but no
 
9:26 PM
Yes, it would!
But alright.
Thank you!
 
There is no "null coalescing operator" in Python; the existing code is how you're expected to write it. If you find it unwieldy, consider redesigning so that you don't get into that situation. For example, consider en.wikipedia.org/wiki/Null_Object_pattern
 
side note: you should use Optional[subprocess.Popen]
 
@Aran-Fey too bad it doesn't have a Ylvis operator
 
Or is | None better on newer Python?
 
Oh, Andras, thanks for that, much nicer!
That's python 3.10 I guess?
I can only use 3.9 right now.
 
9:28 PM
@WayneWerner ringdingdingdingdingdingdidingdiding
might be one too many ding
 
Python is good
IMO
 
@KarlKnechtel is that a reasonable thing in Python?
 
I think it is, just not as explicitly. You do it automatically every time that, for example, you decide not to special-case your string-returning code to return None instead of an empty string. Special cases aren't special enough to break the rules.
Choosing the right approach in context, requires context. I have no idea why the program is storing subprocess.Popen instances in the first place, much less why they'd be optional.
 
@KarlKnechtel Sure, duck typing based on truthiness is fine. A glance at that wiki link made me think you'd want to define a dedicated null-like object.
 
yes, well, that's the problem with "design pattern" formalisms: they're written for languages that need them :)
 
9:33 PM
Because I'm having a class called SecretNameContext() that is responsible for launching an application, including terminating it.
 
@KarlKnechtel said the person linking a design pattern :P
 
+ some other features.
 
@KarlKnechtel So Python doesn't need them?
 
yes, because the underlying concept still helps you write code in that language.
 
So if I terminate, I wanna set the _process to None after I've terminated it.
Do you think I should redesign it?
 
9:35 PM
there is a distinction between Design Patterns(TM) = code that you crib from the GoF book if you're "lucky" enough to be writing C++

and "design patterns" in the sense of what XP/Agile people mean when they say "I have that pattern"
 
@Warcaith you could offload some of the unwieldiness by defining a dedicated attribute that tells you if the context is still active or whatever. Then you can do if self.active: # use method. It would amount to just checking that private attr, but it might read better. The price is that you have to keep the attrs in sync.
 
@Marco Python doesn't need many of them.
 
e.g. "Strategy" in Python = your class inits a callable attribute, and you call it to implement some behaviour
 
There are also named patterns that are part of the batteries that are included e.g. decorate-sort-undecorate.
 
I thought that design patterns were important in all languages
 
9:36 PM
yes, that's a good non-GoF example. the key argument to sort is informed by the Perl idiom
 
Can't wait for the day when design patterns are a thing of the past
 
I would expect the world to move in the direction of design pattern proliferation. Pretending to be smart without saying something substantial. The perfect paradigm.
 
I keep reading messages like "don't do it that way" (in Python) (although it works), etc.), I thought this sort of thing was also due to design patterns
 
also great shibboleths to weed out like-minded smart people who know the bestest patterns
 
@Warcaith After the process has terminated, why is the code still hanging on to the SecretNameContext instance?
@Marco again: difference between code and concepts
 
9:38 PM
@Marco pattern as in "anti-pattern" is a bit different from "named Design Patterns"
 
Oh, ok
So anti-patterns are not included in Design Patterns
 
@KarlKnechtel I see what you mean. I could perhaps start the process inside the init instead and just kill it inside del, instead of heaving separate launch() and terminate() functions.
Like, let the lifetime of the class be responsible of the launch and termination
 
@Marco no, anti-patterns are just a part of folklore, referring to common mistakes that appear in specific circumstances and which have much better ("idiomatic") alternatives that the language offers
 
well also like
"named Design Patterns" capture an *idea about how to write code*
people confuse this for the code examples, because ... thinking is hard I guess
let me give another example. In the GoF book, a pattern is described named Factory.
 
@AndrasDeak--СлаваУкраїні Right
 
9:40 PM
@KarlKnechtel Yes, labels are helpful when they help you organise notions. But when labels become the target you know you've gone too far.
 
In Python, the equivalent is... a staticmethod that returns an instance of the class.
 
@KarlKnechtel I got it
 
You are still, in a very meaningful sense, "implementing the Factory design pattern" by doing this
 
@KarlKnechtel Yeah, I understand
So there is no Python's design patterns?
 
that depends on what you mean by the term :)
 
9:42 PM
Named Design Patterns
 
Design Patterns weren't really meant to be language-specific in the first place. The authors of the book with that title used C++, and the fact that they used C++ informed their mental model of what is a pattern that needs highlighting
 
Yeah, I know that is not language-specific
 
You can write a guide, and copy and paste all the "problem" and "context" pre-amble for the Factory pattern from the original book, and then show two lines of Python code. It shapes your thinking about problems
but it doesn't give you a whole code structure to emulate, because it would be inappropriate to do that. You just write the staticmethod. The insight is "hey, here is a common use case for staticmethod, lined up with a common problem that is solved in such a way"
 
I doubt most experts think in terms of named design patterns when solving problems
 
Yeah
 
9:45 PM
maybe super-high-level ones like MVC
 
I doubt it as well! I certainly didn't, even immediately after reading the book, even though I also used c++ a fair bit at the time :)
However, it's a teaching tool
 
I'm surprised by all this
And relieved at the same time
 
@KarlKnechtel it should be made clear in my opinion that being a good coder doesn't need you to know many named patterns, and knowing lots of named patterns doesn't make you a good coder
 
Design patterns are boring
 
you show the student "hey, you know that staticmethod thing from that Raymond Hettinger video lecture the other day? Did he show how you can make "named" constructors for it and thus instantiate the class with different sets of arguments? No? Oh, let's have some fun today."
and then eventually, it is second-nature to the student. A name is not needed for the concept; it is just known.
 
9:47 PM
Knowing patterns is important. Names are often arbitrary.
 
@AndrasDeak--СлаваУкраїні you mean patterns that are not the named design patterns ones, right?
 
exactly. We pick names so that we can communicate about the patterns we know.
 
@Marco patterns, whether or not there's an accepted or niche name for them
 
@Marco I don't think there is a real distinction.
 
Well
 
9:49 PM
# for instance you might do
sentinel = object()
def foo(bar, baz=sentinel):
    if baz is sentinel:
        # nothing was passed
        ...
    elif baz is None:
        # this is valid input now
        ...
 
The GoF patterns are only "special" because a lot of people bought the GoF book and because a lot of people used C++ when it was published, and because programming educators at the time weren't really thinking in terms of "you know, it's really C++'s fault that we have to write all this boilerplate for the problem"
4
 
You might call this a sentinel pattern, you might not, I have no idea. What matters is to know that you can use an object instance like that as a sentinel value.
 
Java also uses a lot these Design Patterns
 
(when someone says "sentinel" I expect it to mean specifically that such a special value is used to mark the end of a sequence)
(like for example with the two-argument version of iter)
 
I'm really confuse right now: python-patterns.guide
"Python Design Patterns"
 
9:52 PM
you know what, I really had better get started on my errands before I get into talking about Java :)
 
Seems you like Java a lot :P
 
@Marco huh, I'm not familiar with this author, and the writing style is quite good actually. but I really do need to get going >_<
 
Yeah
See you later
 
Surprisingly good website, but I'm not so sure about using matplotlib as an example for the builder pattern...
 
Maybe I should reset my brain now considering this chat about Python (Design) Patterns, now that I saw this website?
@Aran-Fey Yeah, it seems good (maybe would be nice to use design patterns in Python). I can't say anything about use builder pattern concerning matplotlib.
 
10:19 PM
should I*
 
00:00 - 21:0021:00 - 23:00

« first day (4233 days earlier)      last day (708 days later) »