* unpacks a tuple and ** unpacks a dict."}},{"@type":"Question","name":"Is this valid in Python and why?
def my_function():
print my_function.what
my_function.what = \"right?\"
my_function() # Prints \"right?\"
","acceptedAnswer":{"@type":"Answer","text":"It is valid. In Python, everything is an object, including functions. But if we don't have what defined, we will get an Attribute error."}},{"@type":"Question","name":"Given variables a and b, switch their values so that b has the value of a, and a has the value of b without using an intermediary variable.","acceptedAnswer":{"@type":"Answer","text":"a, b = b, a"}},{"@type":"Question","name":"How would you xor in Python?","acceptedAnswer":{"@type":"Answer","text":"
(a and not b) or (not a and b)
bool(a) != bool(b)
bool(a) ^ bool(b)
from operator import xor xor(bool(a), bool(b))
"}},{"@type":"Question","name":"What is introspection/reflection and does Python support it?","acceptedAnswer":{"@type":"Answer","text":"Introspection is the ability to examine an object at runtime. Python has a dir() function that supports examining the attributes of an object, type() to check the object type, isinstance(), etc. While introspection is passive examination of the objects, reflection is a more powerful tool where we could modify objects at runtime and access them dynamically. E.g. setattr() adds or modifies an object's attribute; getattr() gets the value of an attribute of an object. It can even invoke functions dynamically - getattr(my_obj, \"my_func_name\")()"}},{"@type":"Question","name":"One thing about python OOP is that it doesn't support true private attributes/methods. How do we circumvent this limitation?","acceptedAnswer":{"@type":"Answer","text":"There is a convention if we prefix the name with an underscore, it's considered private."}},{"@type":"Question","name":"What's a Mixin?","acceptedAnswer":{"@type":"Answer","text":"Mixin is a concept in Programming in which the class provides functionalities but it is not meant to be used for instantiation. The main purpose of the Mixin is to provide functionalities which are standalone and it would be best if the mixins themselves do not have inheritance with other mixins and also avoid state."}},{"@type":"Question","name":"What's a metaclass?","acceptedAnswer":{"@type":"Answer","text":"In Python, everything is an object, including classes. And just like any other object, classes are instances of something - a metaclass. The default metaclass is type."}},{"@type":"Question","name":"What are the Dunder/Magic/Special methods in Python? Name a few.","acceptedAnswer":{"@type":"Answer","text":"Dunder (derived from double underscore) methods are special/magic predefined methods in Python, with names that start and end with a double underscore. There's nothing really magical about them. Examples of these include:
__init__ - constructor
__str__, __repr__ - object representation (casting to string, printing)
__len__, __next__... - generators
__enter__, __exit__ - context managers
__eq__, __lt__, __gt__ - operator overloading
"}},{"@type":"Question","name":"What is PEP8?","acceptedAnswer":{"@type":"Answer","text":"PEP8 is a generally recognized style guide for programming in Python. It covers formatting, comments, naming conventions, but also programming recommendations as well as useful tips on various topics. The main aim of PEP8 is to help developers improve code readability, reliability and maintainability."}},{"@type":"Question","name":"What is pip?","acceptedAnswer":{"@type":"Answer","text":"Pip is a Python package management system. It provides means of installing, versioning and freezing Python software packages and libraries from the main CheeseShop or third party repositories."}},{"@type":"Question","name":"CheeseShop? What is that?","acceptedAnswer":{"@type":"Answer","text":"CheeseShop is what the python developers call PyPI (The Python Package Index) - a repository of software for the Python programming language."}},{"@type":"Question","name":"What are virtualenvs?","acceptedAnswer":{"@type":"Answer","text":"A virtualenv is what Python developers call an isolated environment for development, running, debugging Python code. It is used to isolate a Python interpreter together with a set of libraries and settings. Together with pip, it allows us to develop, deploy and run multiple applications on a single host, each with their own version of the Python interpreter, and separate set of libraries."}},{"@type":"Question","name":"What are modules and packages in Python?","acceptedAnswer":{"@type":"Answer","text":"A module in Python is any file with .py extension. A package is a set of modules - a directory that contains one or many .py files, one of which is __init__.py Both modules and packages define functions, classes or any kind of software functionality. When we import something in Python, we import from packages and modules."}},{"@type":"Question","name":"Bonus question: Tabs or Spaces?","acceptedAnswer":{"@type":"Answer","text":"Author is a spaces zealot. 4 spaces per tab that is. Both answers are correct (spaces is more correct ?). Incorrect answers are \"I don't know\", \"I don't care\", \"Both\", \"Does it matter\"."}}]]}]