Python Concept
Question 1
What is Python really? You can (and are encouraged) make comparisons to other technologies in your answer
Here are a few key points: -
Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that. You can do things like
x=111
and thenx="I'm a string"
without error
Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++'s
public
, private
), the justification for this point is given as "we are all adults here"
Writing Python code is quick but running it is often slower than compiled languages. Fortunately, Python allows the inclusion of C based extensions so bottlenecks can be optimised away and often are. The
numpy
package is a good example of this, it's really quite quick because a lot of the number crunching it does isn't actually done by Python
Python finds use in many spheres - web applications, automation, scientific modelling, big data applications and many more. It's also often used as "glue" code to get other languages and components to play nice
Python makes difficult things easy so programmers can focus on overriding algorithms and structures rather than nitty-gritty low level details
Question 2
What does this stuff mean:
*args
, **kwargs
? And why would we use it?Answer
Use
*args
when we aren't sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargs
is used when we dont know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args
and kwargs
are a convention, you could also use *bob
and **billy
but that would not be wise.Question 3
What do these mean to you:
@classmethod
, @staticmethod
, @property
?Answer Background knowledge
These are decorators. A decorator is a special kind of function that either takes a function and returns a function, or takes a class and returns a class. The
@
symbol is just syntactic sugar that allows you to decorate something in a way that's easy to read.
Python and multi-threading. Is it a good idea? List some ways to get some Python code to run in a parallel way.
Answer
Python doesn't allow multi-threading in the truest sense of the word. It has a multi-threading package but if you want to multi-thread to speed your code up, then it's usually not a good idea to use it. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your 'threads' can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread. This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core. All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn't a good idea.
There are reasons to use Python's threading package. If you want to run some things simultaneously, and efficiency is not a concern, then it's totally fine and convenient. Or if you are running code that needs to wait for something (like some IO) then it could make a lot of sense. But the threading library wont let you use extra CPU cores.
Multi-threading can be outsourced to the operating system (by doing multi-processing), some external application that calls your Python code (eg, Spark or Hadoop), or some code that your Python code calls (eg: you could have your Python code call a C function that does the expensive multi-threaded stuff).
Question 4
What is monkey patching and is it ever a good idea?
Answer
Monkey patching is changing the behaviour of a function or object after it has already been defined. For example:
import datetime
datetime.datetime.now = lambda: datetime.datetime(2012, 12, 12)
0 comments:
Post a Comment