You're not going deep enough into reverse psychology. Surely "pls no downvote me cuz im new to phyton" is a better way of getting a downvote, and therefore a pity upvote, than asking for a downvote?
I wonder if the top distribution looks like a power law graph, or in other words a few active ppl answering, with a long tail of those who answer every now and then, or just once.
Though I did answer a bad question in the beginning (technically an incomplete one) I quickly got to 1k within around a month and a hafl
But I am mostly an answerer. All my answers are quite young so there aren't high-scoring answers. Most of them are also quite minor, as I am willing to help beginner users to get into it
Note that / in python2 still means integer division when both operands are int. But in his program there is an error when he uses / to cut decimals, so only // is used, hence python3
I finally got some feedback from the OP of that "singleton" question:
Many thanks. The problem with this approach is that I have to assign all the methods from the class one-by-one to some local variable and thus I would still need to copy paste everything. I would like to avoid code duplication. — Jerry30 mins ago
I thought of a way around that, but it involves updating globals(). I'm really not keen on doing that, and I bet Wim wouldn't be happy either.
globals().update({k: getattr(_myclass, k) for k in _MyClass.__dict__
if not k.startswith('__')})
You'd probably need to do that in another module or in the __init__.py though. If you try to make an import hook in the module itself, it's probably already too late
@Aran-Fey That's the traditional definition. It's a big deal in some languages, like Fortran or Pascal. C made the "radical" decision to not distinguish procedures from functions, and most languages since then have followed that trend.
@AndrasDeak True. I guess "procedure" is more a theoretical CS term, which Pascal adopted.
@AndrasDeak Kind of. In Python, it's easy to add a function to an object after the object's defined. In most languages, that's not easy, and may be impossible. It works in Python via the descriptor machinery: every time you do someobj.mymethod a fresh bound method is created.
Folks, there doesn't seem to be anything vaguely canonical on 'How to install specific version of pip?' or 'How to downgrade pip version?' Re stackoverflow.com/questions/50462578/…
and moreover that question says 'pip3' everywhere and nowhere 'pip', except in the tag
I have a custom Path class and I'm trying to define a "parent()" method, but getting an error saying "Path object is not callable". I've checked if "parent" word is a keyword, and it's not.
If I name it to parent1(), it works. So what's the problem here?
@akinuri sounds like you have a name clash with something else called parent in your scope. Probably one of your imports, or a custom function. Try rerunning that in a clean shell.
@IMCoins Yes, totally. There's a huge performance difference between 2048 (GPU) cores vs 8 or 16 (CPU). (For the set of ML tasks whose memory footprint can fit onto GPU, like NN, deep NN, GBM, parallelized RF). This is why sklearn is seriously losing ground (for that set of tasks)
Can anyone help me come up with a very simple example of multiple inheritance, but with two independent base classes? (i.e. two classes that aren't designed for cooperative inheritance with a constructor that calls super().__init__(**kwargs), and aren't mixins)
I'd say it's a typo, if I was sure that your diagnosis of the problem is correct. Does opening the file in append mode really re-write the file headers? That would be stupid.
It's not exactly a typo, just a dumb thing to do. gzip allows you to write to a single file in "block mode". But it's best avoided if you have a choice as it's not very efficient, due to the header duplication.
There are other compressors that are designed for more efficient streaming compression, for when you really need that.
@ShivamKumar Normal "w" mode is fine here. "a" mode is only needed if you want to append fresh data to an existing file. I sometimes see code where a file is repeatedly opened in a loop in "a" mode, a line is written, then the file is closed, on every loop iteration. That's very inefficient, and should only be done if the data must be written in a fragile environment where the system is constantly in danger of crashing. And even then you risk data corruption anyway... — PM 2Ringyesterday
If you need to constantly open/close a file, wouldn't it be better to just use a proper database ? If the program/computer crashes, the database will still be there, ready to use.
hi guys, quick freshman question - why am I getting 'list index out of range' error, when trying to get list value by accessing it like tags[typeid], where typeid is int(tag.type) (returns integer 1) and the same attempt with tags[1] works well?
For the record, I really do not like the idea of adding inline assignments to Python. Sure, I thought it was cool when C introduced it, but it can easily lead to unreadable code, and I thought that Guido was smart not to put it in Python.
@PM2Ring On the other hand all answers to that one seem to swap the relationship. OP's code would've produced {'andrew': ['george', 'judy'], 'fred': ['andrew', 'judy'], ...}.
And that accepted answer has one of my pet peeves: the redundant .strip in line.strip().split(). And they've called it twice in that yamming dict comp. :(
@Arne From the docs: "csvfile can be any object which supports the iterator protocol and returns a string each time its next method is called -- file objects and list objects are both suitable. "
@jpp Fair point. I sometimes add a sentence: "I'm reading the data from a list of lines, to keep the demo simple." Presumably the OP knows how to loop over a list and how to loop over the lines of an open file.
@jpp Yeah, StringIO does have that advantage. But if the OP is a newbie, StringIO may be a bit too advanced for them.
It's introducing a language feature that's not relevant to their problem, and they shouldn't have to learn about it just to understand your solution.
Of course, there are times when the OP needs to learn about StringIO or BytesIO because they're the appropriate tool for the job, but the OP doesn't even know they exist.
@jpp Maybe add a tiny bit of explanation: "StringIO lets us read from a string as if it were a file". I'd suggest linking to the docs, but the io module docs are evil, and I'd hate to inflict them on a newbie.
Need a bit of mentoring re SO questions. This question feels it needs my downvote because he didn't seem to research more on range() and debug his code enough. This is subjective though. Is that enough justification to merit a downvote?
Hmm, if I downvote, then, based from what I learned in Psychology class, it could motivate him to get better and figure out the solution to such a relatively simple problem.
Given that OP is reading a Hacker Rank page that teaches how if blocks work, I'd be surprised if he knew where to look in order to determine what something does. For that reason, I gave him some slack.
You don't have to be the Mozart of code to notice "hey, my code looks different in my question compared to the code in my editor, maybe I should fix that"
You don't even need to know that Python has significant whitespace
@SeanFrancisN.Ballais Oh dear. When I see code like that, where the OP is using a basic language tool like range in a totally weird way, I figure that they have some crucial misunderstanding. They could read the relevant stuff in the docs ten times, and it probably won't help. They really need some teacher - student interaction to set them straight.
And an SO answer isn't designed for that, although it may be possible to do it in Chat. Generally, the best you can do in an answer is to say "X doesn't work like that, you need to do Y".
On a similar note, I guess that this OP has heard that classes are good, but he doesn't really know what he's doing, and it's hard to know where to start to help him. Apart from the good old "nuke the entire code from orbit, it's the only way to be sure".
@RodrigoSilva, for me, docs only give you information as how a specific construct or keyword works and how you can use it. It doesn't teach you how to use a language properly.
If you're a newbie with a crucial misunderstanding then simply reading the docs by yourself is of limited value because you'll continue to misunderstand what the docs are telling you. If you're not a newbie, you should know how to test your knowledge by writing little bits of code. And if the code doesn't do what you expect, you keep trying stuff until it clicks. But newbies don't have a big enough foundation to do that sort of thing with confidence.
I don't care much for arguments of the form "we must allow bad behavior X, because otherwise programmer need Y will go unfulfilled". It isn't Stack Overflow's goal to fulfill all programmer needs.
If it's a simple, common misunderstanding, eg for x in mylist: a = mylist[x] then an answer can explain why that's wrong, and the OP and many future readers can benefit. But if it's some weird thing that the OP's done that others are unlikely to do, then writing a mini-tutorial is only going to benefit them, and so it's not a good fit for SO.
@AndrasDeak I don't see the correlation. You might need a mini tutorial for something that's truly specific, for instance, I remember one really popular SO questions being something along the lines of "why is an array faster..." and the guy who answered went to town on branch prediction and all that
@RodrigoSilva People search SO for good answers. The questions are "merely" a catalyst for those answers, and a way to help people find the answers. If an answer only helps a single person, it's not of much value to the site. Think of the question as illustrating a specific case that the answer is providing a general solution for.
If you haven't already read it, I urge you to read Optimizing For Pearls, Not Sand. Admittedly, the attitude of the powers that be has shifted a little since Jeff wrote that article, but it's still a strong principle for many SO regulars.
@PM2Ring Hm, I can partially agree with that. Though, should we take a question's chance of incidence (is this a word in English?) as our metric of a question's worth?
For example, take one of my first questions on SO: stackoverflow.com/questions/18704612/… I could have easily found the answer somewhere in the documentation, but it helped around 4k people. Does that make it valid? I definitely didn't check all SO question formatting guidelines.
A classic example is a question where the OP is looking for a regex. Sometimes that leads to good answers because they can be helpful examples to most people learning how to do regex stuff. But in many cases it involves writing some unreadable regex that's only of use to the OP, so it's not really a good question.
In what instance should I favour building a script into a class over a module which just gets imported? I've wrote two versions of code which do the same thing (bar the class version returns a dict, which I can easily do in the module as well..) Could someone please tell me which implementation they would prefer? https://gist.github.com/samjtozer/469461bb81e941d63f280b19e77147ab. https://gist.github.com/samjtozer/a3fff81fc44f1baefda80e6dde9f6e87.