Yes, that's because k.func() is calling a method on the k object, so k is self.
But A.func() is calling the method on the class. There is no instance to be passed as self. (You can pass one manually, by calling A.func(k), but that's not usually something you want to do.)
This is actually identical to C++, except than in C++ you'd have to write A::func() instead of A.func(). For A().func and k.func() the syntax is identical for the two languages.
I think you may have been confused because you've stripped your example down a little too far. In a real-life class, instances actually have some meaningful identity and state, and methods do something with that. For example, it's pretty obvious that you can't call read on the file class, only on a file object.
Well, C++ has multiple ways of doing the same thing. If you want to call the default constructor, you can write A k; or auto k = A();` or auto k = A{}; or A k = {}; or a fw more things, but of course not the most obvious thing A k();, because that declares a prototype of a function returning an A.
That's one of the tricky parts of using Stack Overflow—you need to strip something down to a minimal example to get help, but it's easy to get so minimal that it becomes too abstract to think about clearly…
Turns out I still can't comprehend what Martijn's trying to tell me in his comment even after a good night's sleep. Great. Time to give up on trying to read the source code and bring out the debugger...
hi, i have string, ex. q = '''CONCAT(first_name, " ", last_name) AS "FullName", field1, field2''' and i want split it so ['CONCAT(first_name, " ", last_name) AS "FullName"', field1, field2] , can u help me?
If you're now thinking "but that gives me ['CONCAT(first_name, " ", last_name) AS "FullName"', 'field1', 'field2'], I want ['CONCAT(first_name, " ", last_name) AS "FullName"', field1, field2]. See how there's no apostrophes around the final two elements?" you can't have a string inside a list that has no quote marks when you print the list. You would have to create a custom type that overrides __repr__.
You might have to write your own parser. If a string contains parentheses pairs and you want to treat data inside parentheses differently than data outside parentheses, then that's a classic red flag that ordinary string manipulation approaches aren't powerful enough to do the job
Or more generally, tokens having context sensitivity is a big deal breaker. In other words, you can't say "any word immediately after a comma is a field name" so any solution based on searching for commas will necessarily have a ton of extra logic stapled onto it
How to find all the numbers in the following string in python is a curious question; OP spent 58 characters in a comment explaining that data_spe is the name of their list, when they could have done it in 9 characters by just sticking data_spe= in front of their first code block
Jimmies moderately rustled that I wrote a beautiful answer for that question before noticing that they also need "23 42 blah" to get matched as "2342" which I'm pretty sure means that there's no solution that's just pure regex
I am facing a very basic problem using directory path in python script. When I do copy path from the windows explorer, it uses backward slash as path seperator which is causing problem.
>>> x
'D:\testfolder'
>>> print x
D: estfolder
>>> print os.path.normpath(x)
D: estfolder
>>> print ...
A recent question asks "how to compare the values of two objects, not their type?" and it made me madder every time I looked at it so I had to close the tab
3 and "3" do not have the same value in the sense that they are stored as the same sequence of bits in memory. If you discover a method that magically compares two object's values while ignoring type, and expect magical_compare(3, "3") to return True, you're going to have a bad time.
If you want to determine whether two objects compare equal when they're both converted to integers, that's fine, but you should ask for that and not this
A re-indentation algorithm seems like a fairly big project even if you're allowed to simply crash in ambiguous cases. If they want someone to write a solution, it's too broad; if they want to know if a solution exists out there somewhere, it's a resource rec
I am happy to be proven wrong - if anyone has an implementation on-hand that can fit inside an answer, I'll cast a reopen vote
And who's to say that "use the least amount of indentation" will give the intuitive answer most of the time. Or any other simple heuristic along those lines
I don't doubt that an indenting algorithm is possible, but I do doubt that it would be useful
If there is a simple and elegant solution, we should reopen. If the answer is the straightforward, sweaty implementation of an arbitrarily constrained interpretation of the question, we should not
@vishnumc welcome, please read our room rules: sopython.com/chatroom, in particular don't post recent questions, especially with no further context. Also, don't create multiple accounts.
For the auto-indent thing, there's an even bigger issue than chained ifs as @Kevin mentioned here or for/if/else as I mentioned in a comment on the question. Even this is ambiguous: 'if a:\nprint(1)\nprint(2)\n'. Is that second print part of the if or not?
The only option is to always guess rightward unless it's illegal. And if you want to see how that works, try pasting any code character-by-character into either iPython or emacs python-mode.
@vishnumc Assuming that we all already know the answer seems highly unreasonable. We would have to do the same google search that you can do yourself. Google knows all the answers!
@Code-Apprentice google is not an intelligent guy. It just become intelligent through us. Google can provide knowledge which is on any server and it may written by you or me or anyone else. As you said " google knows all " , thats why i am here :-)
Google becomes intelligent through people posting intelligent things—e.g., writing good questions and good answers on StackOverflow. It becomes stupid through people posting useless questions that make the good questions and answers harder to find.
@wim Haven't you ever seen a question on SO, thought "Oh come on, that has to be an easy search", done the obvious search, and gotten back the same question you were answering (or another, worse, dup of the same thing) higher in search results than the relevant docs page, blog post, or canon SO answer?
@abarnert I haven't done that, but I have searched for a question, found the answer on SO, tried to upvote either the answer or the question only to get the message, "You can't vote for your own question"
I get that occasionally. It's usually a good indication that the question doesn't have a duplicate, but I try a couple other phrases first just to check.
@WayneWerner I've done that too. I also once tried to downvote an old answer that really wasn't even very good for its time and got the same error message…
True, but Mbps = • raises a SyntaxError, and i saw someone using 0 instead in some StackOverflow answer, and that made my error go away for some reason, so it must be the right answer to my problem.
@cᴏʟᴅsᴘᴇᴇᴅ The Earth is almost an oblate spheroid. Technically it's not even that, but then technically it's not even an ovoid; every overhang on a cliffside means there are lines that intersect the Earth at 4 places instead of 2.
Physicists have a sliding scale of "featureless sphere -> featureless oblate spheroid -> complicated not-entirely-solid with mountains and people and stuff" that they move up and down as befits whatever math they're doing
I think physicists mostly stop at the first one, except geophysicists. If they can assume a spherical cow… (But of course for topologists, that isn't even an assumption; the Earth is a sphere the same way a coffee mug is a donut.)
Geophysicists do care about the deviations from roundness, but that's because most of them get money from oil companies instead of from government grants, and for some reason oil companies are interested in calculating where there's liquid between the surface and the mantle.
According to my one acquaintance who's a geophysicist, the worst thing about the job is that you have to work with oil company execs, scam artists like dowsers, and, worst of all, geologists.
@abarnert I learned about that when a friend/co-worker was calculating distance between two points on a map and he learned that there are heckin ways to do that, and they can all be wrong for some reason or another
FMA also had a second dog homunculus who lived a rich fulfilling life, so the lesson is not "dog homunculi are bad" but rather "don't let second-rate alchemists experiment on your children; first-rate only"
I want to log into a site (SO actually), I found this post, I converted it into Python 3 syntax and from the code I do not seem to have logged in. I am wondering what is wrong.
> A geophysicist is someone who studies the Earth using gravity, magnetic, electrical, and seismic methods. Some geophysicists spend most of their time outdoors studying various features of the Earth, and others spend most of their time indoors using computers for modeling and calculations.
> Some geophysicists use these methods to find oil, iron, copper, and many other minerals. Some evaluate earth properties for environmental hazards and evaluate areas for dams or construction sites. Research geophysicists study the internal structure and evolution of the earth, earthquakes, the ocean and other physical features using these methods.
What if we port TensorFlow to Forth? Then we can get rid of the computer and just run everything on a laser printer/copier/scanner. If we include enough paper trays we can get massive parallelism.
@Aran-Fey Sometimes, explaining to them how to do it is the best way to show how horrible it is. (As long as you leave some gaps in the implementation as an exercise for the reader, because otherwise they'll just copy and paste your horrible code into whatever they're working on, and the next thing you know your bank account is broken, along with a million of your closest friends.)
I have another question. For quick data validation, I'm currently using crc32. What would be the reason to switch to adler32 or just python's builtin hash?
Adler32 is simpler and at least potentially faster. Python's hash function is harder to collide with pathological data (but still not a strong crypto hash).
I think that 1KB isn't quite small enough to break Adler, unless there are common prefixes to many of the messages, but you'd probably want to look that up instead of trusting my guess…
The issue is that you need 256 bytes to traverse the whole sum space once, so at 1K you're probably getting a wide enough distribution.
It's slower. And it's not repeatable, unless you disable hash randomization, in which case it's not much less collidable than CRC32 with pathological inputs.
What are you actually trying to protect against? Is it just error correction for rare disk errors or transmission errors or the like?
Yeah. that's fine. If CRC32 is fast enough, I'd stick with that. If you want to work out your exact reliability requirements and do the research to come up with the fastest thing that meets them, I'm sure it'll be faster than CRC32, and it could be that Adler32 is fine (or maybe Adler32(x) if len(x)>CUTOFF else Fletcher32(x) or something?), but it's probably not worth the effort.
Actually, from a quick check, binascii.crc32 actually runs a bit faster than zlib.adler32 on 1MB strings on my laptop, and almost the same speed on a Google Cloud instance. And we're talking single-digit microseconds for 100K-1M strings either way. So… yeah, I'd just CRC.
Ah, on my laptop, zlib.crc32 is actually not as fast as binascii.crc32. Weird. Maybe that's because zlib is built against the system libz.dylib, and Apple didn't bother to optimize it, but binascii is using code in Python that is a bit optimized?
(My GC instance is Ubuntu, and it seems less likely they'd include a less-optimized libz.)
Is that a physical linux server? I could imagine most linux distros have a zlib that's better optimized than Python's binascii, while Apple has one that's worse optimized, but meanwhile on my cloud devbox I'm not getting the advantage of most of those optimizations for some reason? (I've seen some weird stuff, like the CPU claims it does AVX2 but when you try to use it things go slower instead of faster…)
I'm guessing that's it then: Linux zlib (with either default configuration or Ubuntu's configuration) probably takes advantage of SIMD or other useful stuff on real machines, but is smart enough to know not to try it on some blacklist of virtual setups, while Python's implementation probably hasn't been updated in years, and Apple's is probably forked from an ancient version of FreeBSD and also not updated in years…