4 ms·
Python Interview Questions. Part II. Middle
- eesmith 8y agoSome of these questions cannot correctly be answered by someone with only "Middle" experience in Python. "Let A and B be objects of class Foo. What functions and in what order is called when print(A + B) is executed?" That's pretty complicated. Does Foo implement __add__ or __radd__? Are either of them descriptors? Does Foo implement a metaclass? Do I assume that the addition will return a Foo? The "Is there an assignment operation in python?" depends on if PEP 572 is considered a definition of what's in Python. Issue35224 is currently in patch review, so perhaps soon it will be in master. The "How are Dict and Set implemented internally?" depends on the implementation. Are we talking pre- or post- 3.6? I only know the pre-3.6 implementation, and even then only vaguely. "How memory is managed in Python?" is a very complicated question. There's reference counting and cyclic garbage collection and free lists. And special memory management during program exit. Likely more. "You have a function that takes other functions as an argument. How to validate the value of an argument?" I have no idea what this means. I think it wants the magic word "callable", but callable() only checks to see if something appears callable. The documentation says 'If this returns true, it is still possible that a call fails'. "You need to implement a function that should use a static variable (for example, a call counter). You cannot write any code outside the function and you do not have information about external variables (outside your function). How to do it?" What does this mean? Python doesn't have static variables. Is this asking for something like: def spam(i): spam.call_counter = getattr(spam, "call_counter", 0) + 1 or def spam(i): x = globals() key = "++tDHYHd9udBFG5LT" x[key] = x.get(key, 0) + 1 I would reject either one in code review. With the "Q: Add some code to make it work". It doesn't make sense. What's it supposed to do? And why "I" and "i"? I think this is supposed to give the right answer: Repeater = iter repeater = Repeater('Hello') for i in repeater: print (i) # hello The example with "print x," switched to Python 2. The list() in the following is needless obfuscation: x = [[0], [1]] print(len(' '.join(list(map(str, x)))))