Friday, 1 September 2017

python Q&A

How memory is managed in Python?
  • Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap.
  • The allocation of Python heap space for Python objects is done by Python memory manager.  The core API gives access to some tools for the programmer to code.
  • Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
Tuples
 tuple is a sequence of immutable Python objects.Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
print "tup1[0]: ", tup1[0] #physics
print "tup2[1:5]: ", tup2[1:5] # [2, 3, 4, 5]
 Tuples are immutable which means you cannot update or change the values of tuple elements.
# Following action is not valid for tuples
# tup1[0] = 100;

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement.
del tup1
What are the tools that help to find bugs or perform static analysis?
PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.
 It will verity that 
  • Unused function/method arguments (can ignore self)
  • Unused globals and locals (module or variable)
  • No global found (e.g., using a module without importing it)
  • Passing the wrong number of parameters to functions/methods/constructors
 PyChecker doesn’t just work on the command line. You can also use it directly in your code! All you have to do is import PyChecker at the top of your module, like this:
  • import pychecker.checker
     
 

No comments:

Post a Comment