r/learnpython • u/dipsy01 • Sep 27 '19
If someone told you they know Python, what would you expect them to know?
Or for example, if someone put Python on their resume. What sort of things would you ask to determine what they actually know, or at least instill some confidence in you.
Edit: Well then, looks like I have some responses to go through! And lots to learn! lmao
138
u/lykwydchykyn Author Sep 27 '19 edited Sep 27 '19
I'd expect you to know how to write a basic class, filter and transform a list with a list comprehension, open a file (the right way) and iterate through it. I'd want to know what you'd built in Python, and maybe ask more specific questions related to that.
EDIT: "The right way" to open a file is using a context manager, i.e.:
with open('filename.file', 'r') as handle:
text = handle.read()Before anyone else asks me what I mean. Yes, other ways work, and are even occasionally warranted, but I feel pretty safe in saying the context manager is the preferred approach.
31
u/vectorpropio Sep 27 '19
I'm starting to mess with files. What do you call open a file the right way?
19
u/DireEntropy Sep 27 '19
with open(file, 'r') as f: print( do stuff )
This is a context manager, for context.
3
u/vectorpropio Sep 27 '19
I have doubt when using other libraries. For example python-docx (the python 3 version of docx). When i open a file with
document = Document('existing-document-file.docx')It is using a context manager internally? or i must take care of this? I couldn't find this in particular.
5
u/phigo50 Sep 27 '19 edited Sep 27 '19
It won't be because you need to have the
documentobject open until you're done with it so a context manager here would have no idea what was going on. You need to passdocument.save()to save it or (presumably)document.close()to discard changes. With a context manager, the file object only exists until the end of thatwithblock.with open("file.json") as f: j = json.load(f)Now, you have
j(which is the loaded contents of "file.json") but the file object itself (f) is gone. Or rather, it's not "gone" but, if you try to do anything withfoutside thewithblock, it will raise aValueErrorbecause the file has been closed.1
u/vectorpropio Sep 27 '19
with open("file.json") as f: j = json.load(f)
Now, you have j (which is the loaded contents of "file.json") but the file object itself (f) is gone.
Is this how it work? I think you are describing correctly the abstraction, you open the file in a context manager and forget the file from there on with all the closing and error handling in the with. But actually the data (for a read example) isn't dumped to memory in the execution of the with line, instead it's readed when it's needed.
2
u/phigo50 Sep 27 '19
You typically use a context manager with files to either open a file and read its contents to a variable or to open a file and save a variable's contents to that file. If you open a file and don't load its contents into a variable, then you won't be able to access the contents after the end of the context manager.
Your example, python-docx, will doubtless use context managers under the hood for the file opening and saving methods but they will be 2 distinct context managers, not one covering the whole process.
I'm sure someone else will be able to explain it better.
1
u/vectorpropio Sep 27 '19 edited Sep 27 '19
Thanks. I will read the docs in context managers again and keep with docx.
1
42
u/AfghanRat Sep 27 '19
Doing with open(file=, mode=) as f: however be sure to use the correct mode with what you're doing, with open opens a file and closes it automatically when you're done with your code (no more f.close) I also think it has some other benefits that I can't list
17
u/ericula Sep 27 '19
The main benefit of context managers is that the clean up after themselves even when an exception occurs during the
withblock. Withopenthis means closing the file when leaving the context. For other context managers it could be that they set temporary variables which are returned to their original state when thewithblock ends.13
1
u/lykwydchykyn Author Sep 27 '19
with open('somefile.blah', 'r') as handle: data = handle.read()As opposed to:
handle=open('somefile.blah', 'r') data = handle.read()1
52
u/networking_noob Sep 27 '19
I'd expect you to know how to write a basic class
Am I the only one who doesn't use classes, like at all? I've been doing Python programming about 5 years and have yet to see the usefulness of classes. Everyone takes on different project types though, so maybe I haven't done one yet where class usage is essential.
If I was doing a tutorial type project aka a game with player health or something, then classes would be used I guess. But I'm not sure how many people are making Python games in the real world, or tasked with modifying a car objects color from red to blue, like so many beginner tutorials.
33
u/Werro_123 Sep 27 '19
Well if you're a Django developer and use the built in ORM, then you at least need to build classes for your models. I think the only times that I've used classes heavily are to play nice with an ORM.
33
u/dnswblzo Sep 27 '19
You've been using classes, just not ones that you have written yourself.
list,dict, etc. are built-in classes. Many third party libraries are also implemented using classes. For example, if you use therequestsmodule and you callrequests.get(url)it returns an instance of that library'sResponseclass, which has methods and attributes you can use to work with the response. A class is a nice implementation choice there because they can abstract away a lot of details about what the library is doing behind the scenes by packaging things up nicely in objects.Python has so much built-in functionality and there are so many libraries to handle common use cases that Python programs are often built by writing procedural programs that implement the business logic, and a lot of the work happens "magically" behind the scenes through objects from built-in or third party classes. Writing classes yourself become more useful when you start to build larger applications where it makes sense to create more abstractions, or if you are building a library for other people to use.
8
Sep 27 '19
You’re not the only one. I was also like this for a long time and I guess lots of python people are too. As someone pointed out you are using classes, just not ones you’ve defined yourself.
If they weren’t a good way to organise things then library authors probably would have done something else.
You don’t need to make contrived examples like car inherits from vehicle, but I’d bet there are areas of your code that could benefit from some use of OOP features.
Do you find yourself with functions ending up wanting 15 parameters, some of which are dictionaries that might have funky structure? Do many methods have similar sets of such arguments?
Sounds like maybe an object could be used to bundle up some of these bits of data which are often found together.
Do you have methods that are really only used on one type of thing and not more generally? Sounds like maybe that could be an instance method.
You don’t need to go hard on polymorphism and stuff - but I do think even just as an organisational tool using classes is probably better than you think.
3
u/gazhole Sep 27 '19
I have some really funky nested dictionaries which could probably benefit from being objects and I never thought about it before. Thanks for this!
4
u/Nixellion Sep 27 '19
Objects/Classes can also have dynamic properties and class functions which operate on data stored in itself.
For example you can store just html in Class.html parameter and have Class.text property which does not hold any data but takes it from .html , processes it and returns plaintext. This way you dont have to hold both html and text in memory for one thing.
1
Sep 27 '19
I once rewrote a dirty maths related script that used nested dictionaries and lists, into a classes one. for some reason it became 3 times fsster.
17
u/taiguy86 Sep 27 '19
Same boat. Maybe 3 years in but I use Python to make API calls, load to databases, and then build models. No idea why I'd ever need to use classes.
9
Sep 27 '19
What are your models if they're not instances of a Model class?
2
u/Bardali Sep 27 '19
Not OP, but for me some models are just functions you run on the data. Making a class seems superfluous.
2
Sep 27 '19
I think maybe we're talking about two different kinds of models. If yours is a mathematical model, makes sense that it would be a function; those are typically stateless.
A data model on the other hand is typically pretty stateful, so classes make sense there.
1
u/Bardali Sep 27 '19
A data model on the other hand is typically pretty stateful, so classes make sense there.
Suppose I want to implement the Kalman filter, I just call that function with the "structure" matrices and data as inputs. I am not sure how that would be helped by creating a class "kalman filter" although maybe it could.
2
Sep 27 '19
Are Kalman filters stateful?
1
u/Bardali Sep 27 '19
I am not exactly sure what stateful means in this context. Are there “states” ? Yes. Is there like a special data type ? I don’t think so. But maybe someone smarter can figure that out.
2
Sep 27 '19
State is when the correct functioning of your software depends on particular combinations of values. You're especially stateful when there are combinations of values for which your software won't run correctly.
→ More replies (0)2
Oct 06 '19
Class extensions are awesome.
statsmodel has a weighted descriptive statistics class. I was using an older version at work and needed a recent addition. I was able to create a modified class to have the same functionality between any version my code would run on (assuming numpy arrays as input).
Classes are just objects to can add attributes to. Functions are objects too, but with more limitations.
12
u/snapetom Sep 27 '19
OO has fallen out of favor for interface programming. For example, Go and Rust do not have classes. Putting it crudely, this approach gives you the power of polymorphism without the slippery-slope of inheritance.
5
u/thirdtimesthecharm Sep 27 '19
Got a guide that explains the distinction?
29
u/snapetom Sep 27 '19
In OO/Polymorphism, I can have a function accept a Vehicle object and call that object's honk() method. However a particular instance might be an F150 object, which is the child of a Truck object, which is a child of Vehicle. You need the whole chain (and all the overhead like properties and methods of parents that you don't need) before you can pass it to your original function just to honk().
In Interface programming, my function says accept any object with a honk() method and call it. In Go, you can then create an F150 struct (which is just an object with properties), implement honk(), then pass that to your function. You don't have to implement any other methods that aren't required, and you don't have to bring in all the inheritance chain baggage. F150.honk() would work, Bicycle.honk() would work, Duck.honk() would work.
In both OO polymorphism and interface programming, you're dealing with the bottom of the chain, but with interfaces, there is no chain.
1
u/s7oev Sep 27 '19
Interesting, thank you! And what about some use cases where OOP is the better choice?
1
u/lykwydchykyn Author Sep 27 '19
I feel like I should point out that using classes does not mean you are doing full OOP. Python doesn't have type safety, and it doesn't have other ways of organizing data and callables into a single object (like JS does with its objects, or Go with structs).
1
Sep 27 '19
Yet every built in type (int, str, etc) in Python is a class. If you're using those at all, and you like how they work, you're enjoying the benefits of OOP.
6
Sep 27 '19
I had the complete opposite going for a while. I made fucking EVERYTHING a class I could. At the time it felt like a good way to divide my code into more manageable pieces. After a few months of that, I learned about the "modularity" of python end felt like a retard.
14
4
3
1
u/lykwydchykyn Author Sep 27 '19
I write primarily web services, administration scripts, and simple GUI utilities, and I use classes all the time. Classes are very useful in all kinds of programming as a way of organizing data and logic.
Games are often used in tutorials about classes because it's an obvious use case, but classes are useful in any application that gets beyond a small scale.
1
Sep 27 '19
I use classes all the time. For example, I built a collector and had to use classes to model the metric data, such as network usage in bytes is IN/OUT/ Total (in + out)
1
5
Sep 27 '19
Correct me if I’m wrong but there is no “right” way of opening a file ,it’s just more pythonic and easier code
4
2
1
1
1
u/Arag0ld Sep 27 '19
If someone knew that kind of stuff, what level would you expect them to be at?
1
u/lykwydchykyn Author Sep 27 '19
To me this seems like basic "101-level" stuff. If you're going to work professionally on code, these things should be in your toolbox.
1
u/dieth Sep 27 '19
j = open("file").read()Is this the right way?
1
1
u/lykwydchykyn Author Sep 27 '19
Not really; you should be using
with open(). Others have asked this question, see my responses there.1
u/Voxxey Sep 27 '19
open a file (the right way)
What do you mean "the right way"? With a
withstatement? Or are you talking about using the python libraries associated for that file type; like opening a.jsonfile with thejsonlibrary, or an.iniwith theconfiglibrary?1
47
u/TheHunnishInvasion Sep 27 '19 edited Sep 27 '19
I'd just ask about projects they've done in Python.
I've never been a fan of asking specific questions about a programming language. It makes a lot of assumptions. One person could be primarily using Python for data science, another could be doing web development, another for software engineering ... there's just so many use cases. Moreover, specific questions tend to always focus on the an area that the question-asker is very familiar with, ignoring that the other person probably hasn't worked on that nearly as much.
Plus, if someone asks me about something I did 6 months ago, I've probably already forgotten the details.
I'm also not a fan of typical coding-interview questions, which mostly test someone's familiarity with ... coding interview questions. Which doesn't seem like a particularly important real world skill.
I would go for very generalized questions that give the person answering a lot of leeway. What libraries they used? How they approached a problem? Challenges they faced on a project? Etc. Keep things on their terms but use your own knowledge to assess what they are saying.
4
u/mfumagalli68 Sep 27 '19
I agree. I think that in an interview you are applying for a specific position and the python questions should be limited to that particular context. I'm a data analyst / data scientist, If someone says he/she knows python I think he/she should be able to answer to basic question on numpy or pandas or in general on list or dictionary, cause if you have worked with data surely you came across that libraries and that data structures.
3
u/Sw429 Sep 28 '19
I'm also not a fan of typical coding-interview questions, which mostly test someone's familiarity with ... coding interview questions.
As a college student, I would always hear people say this and just assume (arrogantly) that they didn't know enough about algorithms and therefore weren't good at interview questions.
But the more I've worked in the industry, the more I agree this is not a good measuring stick.
When I applied for the job I have now, I got this crazy graph problem that required tons of steps. It was exhausting, I didn't do very well on it, and I ran out of time. Somehow, I still got the job, and the actual work I do has nothing to do with anything that I was asked to solve in that interview question.
I do get that interview questions like these can be good for seeing how someone solves problems under pressure, and how they tackle a problem they don't know how to solve. But it also feels a little dumb considering I am sure someone else could solve the question better because they spent their entire winter breaks reading "Cracking the Coding Interview," when I spent mine actually making fun web apps with Flask and getting practice actually coding real things.
0
51
u/wronek Sep 27 '19
I find that asking “what do you like/dislike about python” is a pretty good jumping off point. Folks who know python well will have strong opinions about some of the language’s nuances and display a good understanding of the strengths and limitations of python. At the very least they should know what the data structures are and their appropriate use cases.
41
u/TedTheTwonk Sep 27 '19
One thing that springs to mind that I find to be so unnatural and illogical:
data = [1, 2, 3, 4, 5] a = [n for n in data] # [1, 2, 3, 4, 5] b = [n for n in data if n >= 3] # [3, 4, 5] # c = [n for n in data if n >= 3 else 0] # SyntaxError: invalid syntax d = [n if n >= 3 else 0 for n in data] # [0, 0, 3, 4, 5] # e = [n if n >= 3 for n in data] # SyntaxError: invalid syntax21
u/Gillemonger Sep 27 '19
[x for y in z if w].
X is for each element in the array. It should be an expression that is applied to each element. It could be [0 for y in z], [y for y in z], [y * 2 for y in z], [func(y) for y in z], [y if y >= 3 else 0 for y in z].
The "if w" is for filtering the items in the array. It will reduce the number of items that don't evaluate the expression to true. It could be [y for y in z if true], [y for y in z if false] (no items), [y for y in z if y >= 3], [y for y in z if func(y)].
The main difference is that the first is basically applying some expression for each element, while the second is filtering out elements based on the expression.
5
u/ericula Sep 27 '19
What about
d = [n if n >= 3 else 0 for n in data if n < 10]For me, the most confusing bit about nested for loops in list comprehension is the order in which to put the inner and outer for loop:
d = [n for lst in list_of_lists for n in lst]rather than
d = [n for n in lst for lst in list_of_lists]-7
u/Gillemonger Sep 27 '19
[x for y in z if w].
X is for each element in the array. It should be an expression that is applied to each element. It could be [0 for y in z], [y for y in z], [y * 2 for y in z], [func(y) for y in z], [y if y >= 3 else 0 for y in z].
The "if w" is for filtering the items in the array. It will reduce the number of items that don't evaluate the expression to true. It could be [y for y in z if true], [y for y in z if false] (no items), [y for y in z if y >= 3], [y for y in z if func(y)].
The main difference is that the first is basically applying some expression for each element, while the second is filtering out elements based on the expression.
-6
u/Gillemonger Sep 27 '19
[x for y in z if w].
X is for each element in the array. It should be an expression that is applied to each element. It could be [0 for y in z], [y for y in z], [y * 2 for y in z], [func(y) for y in z], [y if y >= 3 else 0 for y in z].
The "if w" is for filtering the items in the array. It will reduce the number of items that don't evaluate the expression to true. It could be [y for y in z if true], [y for y in z if false] (no items), [y for y in z if y >= 3], [y for y in z if func(y)].
The main difference is that the first is basically applying some expression for each element, while the second is filtering out elements based on the expression.
4
u/elbiot Sep 27 '19
if (2, 3) > '5': print "fuck you, no one meant to do this"5
u/PythonicParseltongue Sep 27 '19
python 3:
'>' not supported between instances of 'tuple' and 'str'Is this really a thing in Python 2?
1
1
u/AdventurousAddition Oct 07 '19
Makes sense for it not to work. What does it mean (if it does work in earlier versions?)
4
Sep 27 '19
What the fuck? why does this work?
1
u/elbiot Sep 28 '19
Python 2 defaults to the type name as a string if types aren't comparable. Seems to break strong typing imo.
4
u/wronek Sep 27 '19
I hope you’re getting off python 2, you’ve got 3 months :p
1
u/elbiot Sep 28 '19
Eh we have as long as we want since none of our 2.7 libs are user facing. Heading to 3 slowly though
6
u/efxhoy Sep 27 '19
My personal dislikes:
Inplace operations like sort() feel very wrong. Functions that mutate their arguments at all break my head.
Empty lists and dicts as default arguments persisting for several calls to a function, forcing me to use None and checking for it feels not great.
Packaging, binaries and distribution fragmentation. pip is cool but I need conda for some stuff. Would be nice with a single unified repo and way of packaging.
I do a lot of data work. I still don't get matplotlib, maybe I'm just retarded. Same with Pandas, I never get assignment working properly without SettingWithCopy warning on the first try.
The amazing syntax has spoilt me,. other languages just look so much like... code. Python reads like English.
Likes:
Syntax is so easy to read and write, very few potentially cryptic magic characters, it's almost all english.
List comprehensions and lambdas are great.
Huge adoption means it's easy to find examples and libraries.
Indentation matters. It forces newbies to indent properly, meaning a consistent readable structure across projects. I love it.
3
u/danpie Sep 27 '19
Inplace operations like sort() feel very wrong. Functions that mutate their arguments at all break my head.
Python does at least seem to help clarify the distinction by not returning a value if they are modifying the parameter in place.
1
u/Jorrissss Sep 28 '19
I'm pretty onboard with you regarding bullets 1 and 2, but regarding matplotlib, I completely agree. I do feel like I have a pretty decent grasp of the internals of pandas (and more-so Numpy), but I really don't understand matplotlib.
1
u/potent_dotage Oct 02 '19
matplotlib makes perfect sense if you're coming from a background in MATLAB. I know it's a big library and they have other ways of using it, but unfortunately I feel like a lot of the introductory material is for MATLAB converts.
Incidentally, as someone with a background in MATLAB, Pandas is bewildering to me. For many libraries (e.g. Numpy, GUI libs) my knowledge transfers back and forth well enough, but Pandas is not one of them.
3
u/jwinf843 Sep 27 '19
asking "what do you like/dislike about python"
I think this approach would really only work if they knew python and another language.
1
u/wronek Sep 27 '19
I only write JavaScript under duress but I use python every day so I’m pretty opinionated about things like type checking vs duck typing, the GIL, virtual environment management etc and I expect most people who’ve used python in any meaningful way would be able to have an impassioned conversation on those topics and more.
2
16
u/bhuddamonk Sep 27 '19
I'm reading lots of responses here that arent real world use cases. Sure you can iterate through lists and do compares and talk about classes and inheritance but what I would ask is in what context do they "know" python? Was is a selenium script? If so, I would start asking them about web driver usage with Python. Was it REST calls? If so, which modules did they use and how did they make the calls. How do you specify a GET vs POST call using Requests? Often, when someone says they "know language (x)", the way you really can tell if they actually used and did something productive with said language is by using in context with another technology (web scraping, machine learning, web automation, API testing, etc.).
2
u/Sw429 Sep 28 '19
I agree. I don't care if someone knows some random peculiarity about the language. If you run into it, you can figure it out with a few minutes of googling. It's more important to know HOW to use those tools to actually make something useful. Writing one line of pythonic code is much easier than putting together a whole project.
14
u/Saiboo Sep 27 '19
"What is the crawling speed velocity of a python?". Bonus points if they know this for the African and Australian ones.
7
6
Sep 27 '19
Speaking as a professional with a decade of experience in Python, I would say "knowing Python" is a pretty vacuous statement. What has this person done in Python?
My expertise is in application development, so in an interview, I will ask questions regarding which libraries the applicant has used and how various problems would be tackled. I would then get him on a whiteboard or a laptop (without an IDE) to demonstrate his ability to actually write code - intermediate stuff, like processing a text file into record objects, omitting column and page headers and footers and reporting a list of parsing errors.
I also like to throw in a problem that is very likely to stump the interviewee to see how he deals with that. A big part of being a developer is being able to find the answer, not knowing it off the bat. This makes most people sweat, but the good ones either fess up that they don't know the answer but would look it up to see if there were a library, a recipe, or some documentation out there. If he says "Stack Overflow", he's pretty much got the job.
Remember: programming is less an engineering pursuit than a crafting pursuit. Try to learn something new every day and just write code, trying out different patterns or styles. Learn from people who have more skill. It's the doing that matters most.
5
u/achampi0n Sep 27 '19
Understand what is wrong with:
def fn(a=[]):
a.append(5)
return aKnow what is duck typing is and how to apply it in code.
Similarly can explain what EAFP means and how it shows up in code.
Etc.
2
u/Saiboo Sep 27 '19
Is the problem for fn that it mutates 'a' ?
5
u/Ran4 Sep 27 '19
More specifically, that the default value changes every time you call the function.
It's true that mutating a might also be an issue, but that's more code style than anything else.
2
u/niggatronix Fitty Sep 27 '19
If you initialize a with an empty list in the function definition, it will be the same list used each time the function is called without arguments. So you would expect an empty list each time you run the function, but instead, the list keeps accumulating.
1
1
1
u/Pulsar1977 Sep 27 '19
Similarly can explain what EAFP means
European Association of Fish Pathologists
1
u/achampi0n Sep 28 '19
Easier to Ask for Forgiveness than Permission:
x = {'a': 1} k = 'b' try: fn(x[k]) except KeyError: <do_alt_code>Vs.
if k in x: fn(x[k]) else: <do_alt_code>1
1
u/Basicallysteve Oct 07 '19
So I tested this and found that this seems to be unique to python (not in JS or Ruby at least). Is there a potential benefit to calling the same array in memory for each default use? Or is this just a consequence of the way functions are initialized?
5
Sep 27 '19
I'd expect them to have the Zen of Python memorized in at least two languages, and preferably have it tattooed somewhere on their body. The more visible the tattoo, the more clout.
13
u/DeepWebConspiracies Sep 27 '19
How to reverse a string, one of the online courses made a joke about how they hate when people use a “cheat” command instead of making it loop. Good luck
18
u/TedTheTwonk Sep 27 '19
Just realised I've never used anything other than [::-1] to reverse a string. According to this source (spoiler, shows a bunch of ways to reverse strings) using a for loop is over 12 times slower.
4
u/DeepWebConspiracies Sep 27 '19
I’m glad the one little fun fact I remember from the course actually helped, good luck on your interviews
0
Sep 27 '19
[deleted]
10
u/TedTheTwonk Sep 27 '19
Now that I think about it - absolutely never for my own code. Only when helping others with school assignments on pythontutor.
10
Sep 27 '19
I work with DNA sequences sometimes. We do all sorts of processing with them. Reversing them is fairly common.
0
Sep 27 '19
[deleted]
1
Sep 27 '19
I do about half and half. But my colleagues do a lot more data science. It really depends on the research.
1
u/jorvaor Sep 28 '19
If you feel curious about that field of work, have a look into r/bioinformatics.
5
1
u/PythonicParseltongue Sep 27 '19
I was close to doing it this week. I had a regex with a negative lookbehind with variable width. And
reonly supports negative lookahead with variable width. Butregexsupports it, so still no need.1
u/3sci Oct 05 '19
That's kinda funny, 'cause sometimes, especially when playing with data, it's just about getting the job done quickly. What to do, python is full or little tricks...and who doesn't like the odd trick? '!niw eht roF'[::-1]
3
3
u/njharman Oct 01 '19
What sort of things would you ask to determine what they actually know
level zero Python newb, you can program in it, but you have no deep understanding of what code/libs do.
level one: list comps, generator expressions, the Big O of python's core datastructures: dict,list,set.
level two: you know all the gotchas, you know when why to use slots, why requests, every major std lib, async vs multiprocessing, vs threading vs gevent. You can articulate what GIL, when it matters, and why it usually doesn't.
level three: know what meta classes are, you know about interned strings, know about features added in latest release or about to be in future release. Basically you live and breath Python.
expert level: You've used the language enough to know and be able to articulate the parts of it that suck. You can argue why Python is not suitable for a specific use that it is not suitable for.
12
Sep 27 '19
Show me how you can debug python code without print statement.
22
u/tajjet Sep 27 '19
Not sure whether you mean knowing how to keep track of code on pen and paper or how to operate a debugger and set breakpoints, but either way I feel like this is less of a "knows python" thing and more of a general programming skill
5
5
5
5
5
3
u/rickwaller Sep 27 '19
I do this all the time, how can I get out of it?
3
u/gr8x3 Sep 27 '19
Using print statements to see variables is handy, but I'd advise using logging instead, which essentially gives you debugging print statements that you can toggle on and off with a single line of code. That way you don't have to remove them all when you're done debugging. Many IDEs also come with a built-in debugger, which can let you step through your code one line at a time and view variables as your code executes, which can help you spot where things are going wrong.
Also, assert statements can be really handy for debugging.
2
2
2
u/epogrebnyak Sep 27 '19
Is that possible?
1
u/Sw429 Sep 28 '19 edited Sep 29 '19
Yeah, you can use debugging tools. The real question you should ask is "is that necessary?", because in most cases it is not. Print statements suffice, and a debugger is usually overkill.
Edit: becessary -> necessary
2
u/epogrebnyak Sep 28 '19
Everytime I use a debugger I end up restarting the console. I think the functions are not modular enough when you have to debug often.
1
4
u/WobWoB1993 Sep 27 '19
find the median of two unsorted lists.
15
u/metriczulu Sep 27 '19
From scratch? Because np.median(list_1+list_2) doesn’t seem like a “knows python or not” solution.
20
u/inFenceOfFigment Sep 27 '19
Knowing numpy and some of its methods? Knowing np is a common alias for numpy? Knowing lists can be combined using the + operator? Seems like a good indication of familiarity with Python and its ecosystem (arguably a large part of the language).
6
u/metriczulu Sep 27 '19
Yeah, true. Knowing how to do it from scratch would be more of a general CS question, but doing it with numpy in one line is more about Python specific comprehension.
2
u/WobWoB1993 Sep 27 '19
It's a question that requires open dialog to get to the best solution. for np.median(list1+list2) you've assumed the lists are small. And that you have unlimited memory. As an interview question you can go much deeper than np.median(list1+list2).
2
2
2
2
2
Sep 27 '19
I'm not sure that I've ever seen this without somebody asking the follow-up question about how well they know python oh, but without seeing more specifics on the resume I would assume that they know the basics of python. I would assume that they are able to take someone else's python code, more or less understand it, and make edits to it as necessary. I would also assume that they could do some tutorial level python stuff themselves. I don't think I would assume more than that.
2
Sep 27 '19
If someone were to say that, the first question would be how much. How much generally means the level of general soecialisation in the language for the sake of problem solving. After that, the knowledge should of course be job specific. For example an app developer is expected to be fluent with tkinter or kivy or pyqt5 (python is not very good for app dev)
2
u/Kerbart Sep 27 '19
List comprehensions. Obviously knowing about them doesn’t make you a (good) Python programmer, but not knowing about them certainly flushed you out as inexperienced or “i did two hours of youtube vids last night.” It’s at that nice cross roads of obscure enough to fall outside the intro curriculum yet a standard tool for those who write code on a regular (daily?) basis.
2
u/Voxxey Sep 27 '19
I think the most basic way of telling if someone "knows" python would be by asking them to set up an environment. If I were testing someone myself, I would give them a fresh Windows (or Linux install, whatever they are more comfortable with) and ask them to set up a python environment in whatever way they wanted. I would also list any packages that I wanted to be in the environment.
Since installing python and setting up an environment (or venv if that was necessary) comes with its own complications, If someone can get python up and running on any computer, that would be a good sign they know what they are doing.
I would start an interview in this way. I wouldn't inform the interviewee that python was not installed on the computer, then I would ask them whatever questions I might have and ask them to begin trying to solve them (I personally don't like whiteboard questions).
1
1
u/fahdmirzacloud Sep 27 '19
I only use Python with boto3 & that's all I know and that's what I will tell anyone who asks me. I will list down specific tasks/projects I did with it. Above all I will say that if i don't know it, I am resilient enough to find it.
1
u/tomekanco Sep 27 '19
Be able to explain and use concepts such args, kwargs, yield, collections.defaultdict, itertools.product, functools.lru_cache, itertools, dunders (__repr__, __init__, ...), decorators, classes, lambda, ...
Be able to name his or her favorite modules.
1
u/epogrebnyak Sep 27 '19
Unit tests, code refactoring, dataclasses (proxy for following on Python 3), own projects on github. Beyond just 'knowing python', but would indicate the knowledge is somewhat mature.
1
u/saltyhasp Sep 27 '19
I would expect that they spent at least an afternoon learning Python (that's how long it took me to learn the basics - a very long Sunday afternoon and evening maybe 8 to 10 hours), then 9 months working on a real project using the language. After that I'd expect that they would accumulate 10000 hours of experience in python and related IT skills.
1
u/Nixellion Sep 27 '19
Its the same as "I know <Insert almost any software/language name>". I hear itba lot about Maya, 3ds max, photoshop. It can either mean that person knows the basics, like python syntax or Maya's main UI features. Or it can mean that they know it in and out which is on its own close to impossible.
And in the end its just a tool. You can know every library in the world but not know what to do with them.
So Id expect them to know the basics and if I have any interest in the person I'd keep asking questions to assess what exactly do they know and what they can actually do in Python.
1
u/random_cynic Sep 27 '19 edited Sep 27 '19
"know Python" is very broad. Do you mean the core language? If so, here are some things off the top of my head:
Updated (>=3.6) knowledge of the dictionaries since they are pretty much the backbone of everything in Python. Also lists, tuples, sets and the common methods.
Iterators (they are a fundamental aspect of python and generalizes the classes mentioned in previous point), generators, comprehensions.
The pass-by-assignment or call-by-object (whatever you want to call it) model in Python.
Python scope resolution system (LEGB).
Python exception system (
try-except-else-finallyand hierarchy of exceptions).Strings and bytes and the encodings python uses for different strings.
How the
importsystem works, modules+packages+namespaces.All the regular OOP stuff like inheritance, polymorphism (and duck typing) etc. how they are implemented in python
Some knowledge of the advanced OOP stuff would not be bad like static and class methods, decorators, metaclasses, descriptors etc.
The essential differences between Python 2 and 3. Like it or not, there's still (and will be) a lot of Python 2 code out there. Need to know how to convert them to Python 3.
Last but not the least '
import this' and what it really means.
1
1
1
u/sseymens Sep 28 '19
Python is so diverse. The language is used for deep learning (artificial intelligence) data analysis, backend data, and front end (Django). Also, someone who knows Python can learn JavaScript fairly quickly. The syntax structure is similar between the two languages despite one language used for the sole purposes of front end development (JavaScript) and the other used mainly for backend development (Python).
1
1
1
1
u/manyu_abee Sep 27 '19
I'd expect them to talk about the performance of recursion in Python.
And know dict and set and their relationship to hash data structures
List vs array, set, dict,deque etc etc. If you're using list for everything, it means you're a beginner..not bad, but that's something I'd note.
Conventions - using under under in private functions and stuffs like that.
Slicing
2
u/IlliterateJedi Sep 27 '19
I'd expect them to talk about the performance of recursion in Python.
What kind of answer would you expect for this question?
1
u/sudomatrix Sep 06 '23
I think it was a trick question about whether Python has tail recursion optimization (it does not).
1
u/AtlasAlex Sep 27 '19
I didn’t think Python actually had “Arrays” I must fall into the doesn’t actually know Python category lol
1
-1
0
Sep 27 '19
[deleted]
3
u/Pokyo Sep 27 '19
You would think. My cousin came to me saying he was going to “learn programming.” So I said great, I’ll give you the absolute easiest task I can think of. I told him to get a number from the user’s input, and print a message telling them what number they chose. He could not for the life of him figure it out. Some people can’t google properly
2
1
u/Jorrissss Sep 28 '19
I think this attitude leads to really poorly written and performant Python code.
110
u/eaojteal Sep 27 '19
It's always felt off when I see someone say they "know python", and occasionally when they want to "learn python" without any context. Python can be used in such diverse ways, I think you need to contextualize. I try to say that I'm familiar with python; I might include what libraries I use most frequently, or to solve certain problems.