Python WTF
-
from collections import Container class NotAContainer: def __contains__(self, x): return False not_a_container = NotAContainer() print(isinstance(not_a_container, Container)) # Returns True
The print statement returns true. What the fuck? Is this behaviour okay? I feel super uncomfortable.
Also the code tag does not seem to work at all. What the fuck is up with that?
from collections import Container
class NotAContainer:
def contains(self, x):
return Falsenot_a_container = NotAContainer()
print(isinstance(not_a_container, Container))
#True
EDIT: Formatted code in the beginning.
-
@stillwater Use triple backticks:
from collections import Container class NotAContainer: def contains(self, x): return False not_a_container = NotAContainer() print(isinstance(not_a_container, Container)) #True
(view the raw to see the formatting)
-
@Benjamin-Hall Thanks. Wonderful. But why the fuck does the box that popup when i click on the compose with '?' next to it say that code tag is allowed. What dumbery am I doing here?
-
@stillwater said in Python WTF:
@Benjamin-Hall Thanks. Wonderful. But why the fuck does the box that popup when i click on the compose with '?' next to it say that code tag is allowed. What dumbery am I doing here?
this is text in code tags
It's supported, just turns it red. I know there's also issues with markdown and html tags (something about having the right line breaks), but I'm not sure exactly what.
-
@Benjamin-Hall Now I'm more interested about the code tag than the python problem. Sigh.
-
It seems
isInstance
only looks at the existence of the magic__contains__
method, which is what's going on behind the scenes when you use thein
operator. See https://stackoverflow.com/questions/11575925/what-exactly-are-containers-in-python-and-what-are-all-the-python-containerIn practice, then, all containers will have the
__contains__
magic method. However, when testing whether an object is a container you should useisinstance(x, collections.abc.Container)
for clarity and for forward compatibility should the Container subclass check ever be changed.
-
@stillwater said in Python WTF:
@Benjamin-Hall Now I'm more interested about the code tag than the python problem. Sigh.
Pretty normal around here. Though topic drift by the OP in 5 is ... different... :)
-
@Jaloopa No I understand how this happens but it does not seem right. Is there any arcane reason it is designed this way. Cos I can literally define any class with the ____ contains ____ method and the same method signature and what is definitely not an instance of the Container class is considered one. Is there something the guy with a PhD in Object oriented programming knows that I ought to know for python being designed this way?
-
Is this a case of Duck typing taken tooooooooooo far?
-
@stillwater Looks like it. I'm no expert in Pythong (far from it), and it doesn't seem sensible to me
-
@stillwater said in Python WTF:
Is this a case of Duck typing taken tooooooooooo far?
AKA Python in a nutshell. If you give it a method that matches what it expects, it must be a
duckwitch!
-
I've written production code in python and I was not aware of this behaviour until a couple hours back. This makes me wonder what other WTFs I'm unaware of. I'm getting the heebiejeebies something something thinking about all the python code I've written. Seems so.. fragile in hindsight.
-
@stillwater said in Python WTF:
This makes me wonder what other WTFs I'm unaware of.
There's a whole cesspit of down there.
(BTW, your code example has another nasty; the class doesn't explicitly inherit from
object
. If you currently have to support 2.7, that'll bite you in weird ways that you definitely don't want. Hopefully you're not on 2.7, but it's still in irritatingly many production deployments…)
-
@stillwater said in Python WTF:
Is this a case of Duck typing taken tooooooooooo far?
Ask Ben, I've heard he knows something about it.
-
@stillwater said in Python WTF:
Is this a case of Duck typing taken tooooooooooo far?
I wouldn't say so, since I'm used to C++ templates. But it's inconsistent and therefore still wrong. Python's "abstract base classes" are weird. The feature is useful, but it should have been designed differently.
-
@stillwater said in Python WTF:
Is this a case of Duck typing taken tooooooooooo far?
ABCs introduce virtual subclasses, which are classes that don’t inherit from a class but are still recognized by isinstance() and issubclass(); see the abc module documentation.
-
class TopClass: def __init__(self, name): print("Inside TOP Class") print("{} calling TOP's Init Method".format(name)) class LeftClass(TopClass): def __init__(self, name): print("Inside LEFT Class") super().__init__("LEFT") print("{} calling LEFT's Init Method".format(name)) class RightClass(TopClass): def __init__(self, name): print("Inside RIGHT Class") super().__init__("RIGHT") print("{} calling RIGHT's Init Method".format(name)) class BottomClass(LeftClass, RightClass): def __init__(self): print("Inside BOTTOM Class") super().__init__("BOTTOM") print("BOTTOM's Init Method") b = BottomClass() #Inside BOTTOM Class #Inside LEFT Class #Inside RIGHT Class #Inside TOP Class #RIGHT calling TOP's Init Method #LEFT calling RIGHT's Init Method #BOTTOM calling LEFT's Init Method #BOTTOM's Init Method
Notice how the Left class is calling the Right class' constructor even though the former does not inherit from the latter. There is an explanation but this behaviour seems WTFy to me as well.
-
@stillwater said in Python WTF:
There is an explanation but this behaviour seems WTFy to me as well.
It's just the normal flattened hierarchy of diamond inheritance.
__init__
isn't special in that regard; it's run after the object is created and is very much not a constructor at all. That diamond inheritance is supported at all is perhaps the most part (and is why), but overall you're wrong on this one; there's a lot of other things you could have picked at fairly in the Python object system, but this is a reasonable outcome from its initial premises.(The method resolution order is right; methods are placed in the last place they could belong. Everything else is worse.)
-
@dkf Never used this sort of inheritance ever. It's just weird to see it this way. If this is the exact way all multiple inheritance supporting languages behave then it definitely is wonky.
-
@stillwater said in Python WTF:
If this is the exact way all multiple inheritance supporting languages behave then it definitely is wonky.
Try and figure out a different way that isn't worse.
-
This post is deleted!
-
@sockpuppet7 Deleting everything is a winning strategy with a complex Python program, yes.
-
@dkf If I was that smart, I would have done it.
-
@stillwater said in Python WTF:
from collections import Container class NotAContainer: def __contains__(self, x): return False not_a_container = NotAContainer() print(isinstance(not_a_container, Container)) # Returns True
The print statement returns true. What the fuck? Is this behaviour okay? I feel super uncomfortable.
Also the code tag does not seem to work at all. What the fuck is up with that?
from collections import Container
class NotAContainer:
def contains(self, x):
return Falsenot_a_container = NotAContainer()
print(isinstance(not_a_container, Container))
#True
EDIT: Formatted code in the beginning.
<code>
is an inline tag. If you want preformatted text, use<pre>
.
-
@ben_lubar Or just wrap the whole thing in
```
sequences. That's easier to type on this keyboard at least…
-
@dkf said in Python WTF:
@ben_lubar Or just wrap the whole thing in
```
sequences. That's easier to type on this keyboard at least…Yes.
The weird part to me is that so many people come here assuming that
<code>
will do something different than<code>
does on literally every other webpage just because this is a forum.
-
@stillwater @dkf They did invent a better way, it's java and c# interfaces.
-
@sockpuppet7 said in Python WTF:
They did invent a better way, it's java and c# interfaces.
Those aren't, because they can't construct the diamond inheritance problem. That's because they can't have the diamond inheritance pattern in concrete method implementations (except for Java's default methods, and those require you to be specific if things would otherwise be ambiguous). That doesn't work at all in any language with dynamic definitions (because the cost of figuring out if you're making a problem is high, since you have to examine more than just the current class).
I advise not implementing the same method on both sides of a multiple inheritance diamond, not if you can help it at all.
-
@dkf They aren't the same, but they are better. Anything you can do with multiple inheritance you can do with composition + interfaces, with a few lines of boilerplate connecting stuff. But those things get explicit and much simpler. If you use too much multiple inheritance, zalgo will come, devour your code, then it will devour you and anybody that ever looked at it.
-
@sockpuppet7 said in Python WTF:
If you use too much multiple inheritance, zalgo will come, devour your code, then it will devour you and anybody that ever looked at it.
Also your code will be arrested for bigamy. Or maybe incest.
-
If I knew that the python conventions was
__function__
for special system functions, then why would I ever unintentionally write a function named__function__
I'm not saying that this convention isn't bad, just that OMG UNEXPECTED FUNCTIONALITY is overblowing things.
-
@JazzyJosh said in Python WTF:
If I knew that the python conventions was
__function__
for special system functionsThe ugly bit is that
this()
is public,_this()
is protected,__this()
is private (by “clever” renaming), and__this__()
is fucking public again.Python's just so full of ossified momentarily-expedient-but-really-awful hacks.
-
@dkf said in Python WTF:
Python's just so full of ossified momentarily-expedient-but-really-awful hacks.
Bit like another language that starts with
p
...
-
@ben_lubar said in Python WTF:
@dkf said in Python WTF:
@ben_lubar Or just wrap the whole thing in
```
sequences. That's easier to type on this keyboard at least…Yes.
The weird part to me is that so many people come here assuming that
<code>
will do something different than<code>
does on literally every other webpage just because this is a forum.The weird part to me is that ```languagename doesn't work on WTDWTF, even though it works everywhere else Markdown is employed (including Discourse).
-
@pie_flavor said in Python WTF:
The weird part to me is that ```languagename doesn't work on WTDWTF, even though it works everywhere else Markdown is employed (including Discourse).
ERR_WOMM
bash #include <stdio.h> >>> import whois >>> w = whois.whois('example.com')
text #include <stdio.h> >>> import whois >>> w = whois.whois('example.com')
C #include <stdio.h> >>> import whois >>> w = whois.whois('example.com')
python #include <stdio.h> >>> import whois >>> w = whois.whois('example.com')
-
Current supported list, it appears:
root@ec2a34e43b6a:/usr/src/app# sed /usr/src/app/node_modules/nodebb-plugin-markdown/public/js/highlight.js -e 's/js.registerLangu/\njs.registerLangu/g' | cut -c-40 /*! highlight.js v9.4.0 | BSD3 License | !function(e){var n="object"==typeof wind js.registerLanguage("ini",function(e){va js.registerLanguage("java",function(e){v js.registerLanguage("php",function(e){va js.registerLanguage("objectivec",functio js.registerLanguage("diff",function(e){r js.registerLanguage("coffeescript",funct js.registerLanguage("json",function(e){v js.registerLanguage("css",function(e){va js.registerLanguage("ruby",function(e){v js.registerLanguage("perl",function(e){v js.registerLanguage("python",function(e) js.registerLanguage("markdown",function( js.registerLanguage("apache",function(e) js.registerLanguage("xml",function(s){va js.registerLanguage("cpp",function(t){va js.registerLanguage("javascript",functio js.registerLanguage("http",function(e){v js.registerLanguage("makefile",function( js.registerLanguage("nginx",function(e){ js.registerLanguage("cs",function(e){var js.registerLanguage("sql",function(e){va js.registerLanguage("bash",function(e){v
-
@pie_flavor said in Python WTF:
The weird part to me is that ```languagename doesn't work on WTDWTF,
The list of supported languages is pretty small.
-
@dkf ... why? highlight.js support 143 languages according to their website.
-
@pie_flavor said in Python WTF:
@dkf ... why? highlight.js support 143 languages according to their website.
How large do you want your javascript to be? The idea is to select only the ones that are likely to be used for your website.
Our problem is that quite a few of them are 'likely.'
-
-
@PJH said in Python WTF:
@pie_flavor said in Python WTF:
@dkf ... why? highlight.js support 143 languages according to their website.
How large do you want your javascript to be? The idea is to select only the ones that are likely to be used for your website.
If it fits in Ram without my phone's browser crashing, it's not enough of them.
-
@PJH said in Python WTF:
@dkf said in Python WTF:
Python's just so full of ossified momentarily-expedient-but-really-awful hacks.
Bit like
anotherevery language that starts withp
...
-
@PJH Yeah, I can totally see the use of
apache
,markdown
,ini
,diff
,coffeescript
,http
,makefile
,nginx
, and why you wouldn't wanttypescript
,go
,rust
,swift
,bat
,gradle
,kotlin
,powershell
...
-
@pie_flavor I can also see why one wouldn't want
go
orrust
… and does anyone wantswift
?
-
@pie_flavor said in Python WTF:
@PJH Yeah, I can totally see the use of
apache
,markdown
,ini
,diff
,coffeescript
,http
,makefile
,nginx
, and why you wouldn't wanttypescript
,go
,rust
,swift
,bat
,gradle
,kotlin
,powershell
...I rather suspect laziness in that regard, in that it's probably one of the defaults that was selected for NodeBB.
I've no idea if we can override it (easily) for here.
-
@dkf said in Python WTF:
@pie_flavor I can also see why one wouldn't want
go
orrust
…Really?
and does anyone want
swift
?I've seen a couple instances of it. Apple platforms are major development targets, as evidenced by the fact that we do have Objective-C.
-
@pie_flavor and Swift is way better (except for some legacy API support) than the cancer that is objective c.
-
@Benjamin-Hall said in Python WTF:
the cancer that is objective c.
If only they'd gone with Objective PHP instead.
-
@pie_flavor said in Python WTF:
@dkf said in Python WTF:
@pie_flavor I can also see why one wouldn't want
go
orrust
…Really?
Yeah, we don't want to be encouraging use of either.
-
@loopback0 Why, what's wrong with Rust?