0 votes
in Python by
Why would you use the "pass" statement in Python?

1 Answer

0 votes
by

Python has the syntactical requirement that code blocks cannot be empty. Empty code blocks are however useful in a variety of different contexts, for example if you are designing a new class with some methods that you don't want to implement:

class MyClass(object):
    def meth_a(self):
        pass
<span class="token cVar">def</span> <span class="token cMod">meth_b</span><span class="token cBase">(</span>self<span class="token cBase">)</span><span class="token cBase">:</span>
    <span class="token cVar">print</span> <span class="token cString">"I'm meth_b"</span></code></pre><p>If you were to leave out the pass, the code wouldn't run and you'll get an error:</p><pre><code>IndentationError<span class="token cBase">:</span> expected an indented block</code></pre><p>Other examples when we could use <code>pass</code>:</p><ul><li>Ignoring (all or) a certain type of <code>Exception</code></li><li>Deriving an exception class that does not add new behaviour</li><li>Testing that code runs properly for a few test values, without caring about the results</li></ul></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://stackoverflow.com/questions/13886168/how-to-use-the-pass-statement" rel="noreferrer" target="_blank" title="Why would you use the &quot;pass&quot; statement? Interview Questions Source To Answer">stackoverflow.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 6. What are local variables and global variables in Python?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><ul><li><p><strong>Global Variables</strong>: Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.</p></li><li><p><strong>Local Variables</strong>: Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.</p></li></ul></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://www.edureka.co/blog/interview-questions/python-interview-questions/#WhatarelocalvariablesandglobalvariablesinPython?" rel="noreferrer" target="_blank" title="What are local variables and global variables in Python? Interview Questions Source To Answer">edureka.co</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 7. What are descriptors?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>Descriptors were introduced to Python way back in version 2.2. They provide the developer with the ability to add managed attributes to objects. The methods needed to create a descriptor are <code>__get__</code>, <code>__set__</code> and <code>__delete__</code>. If you define any of these methods, then you have created a descriptor.</p><p>Descriptors power a lot of the magic of Python’s internals. They are what make properties, methods and even the super function work. They are also used to implement the new style classes that were also introduced in Python 2.2.</p></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://www.blog.pythonlibrary.org/2016/06/10/python-201-what-are-descriptors/" rel="noreferrer" target="_blank" title="What are descriptors? Interview Questions Source To Answer">blog.pythonlibrary.org</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 8. What Is The Benefit Of Using Flask?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p><strong>Flask</strong> is part of the micro-framework. Which means it will have little to no dependencies on external libraries. It makes the framework light while there is little dependency to update and less security bugs.</p></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://www.wisdomjobs.com/e-university/flask-interview-questions.html" rel="noreferrer" target="_blank" title="What Is The Benefit Of Using Flask? Interview Questions Source To Answer">wisdomjobs.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 9. Does Python have a switch-case statement?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>In Python, we do not have a switch-case statement. Here, you may write a switch function to use. Else, you may use a set of if-elif-else statements. To implement a function for this, we may use a dictionary.</p><pre><code><span class="token cVar">def</span> <span class="token cMod">switch_demo</span><span class="token cBase">(</span>argument<span class="token cBase">)</span><span class="token cBase">:</span>
switcher <span class="token cBase">=</span> <span class="token cBase">{</span>
    <span class="token cNum">1</span><span class="token cBase">:</span> <span class="token cString">"January"</span><span class="token cBase">,</span>
    <span class="token cNum">2</span><span class="token cBase">:</span> <span class="token cString">"February"</span><span class="token cBase">,</span>
    <span class="token cNum">3</span><span class="token cBase">:</span> <span class="token cString">"March"</span><span class="token cBase">,</span>
    <span class="token cNum">4</span><span class="token cBase">:</span> <span class="token cString">"April"</span><span class="token cBase">,</span>
    <span class="token cNum">5</span><span class="token cBase">:</span> <span class="token cString">"May"</span><span class="token cBase">,</span>
    <span class="token cNum">6</span><span class="token cBase">:</span> <span class="token cString">"June"</span><span class="token cBase">,</span>
    <span class="token cNum">7</span><span class="token cBase">:</span> <span class="token cString">"July"</span><span class="token cBase">,</span>
    <span class="token cNum">8</span><span class="token cBase">:</span> <span class="token cString">"August"</span><span class="token cBase">,</span>
    <span class="token cNum">9</span><span class="token cBase">:</span> <span class="token cString">"September"</span><span class="token cBase">,</span>
    <span class="token cNum">10</span><span class="token cBase">:</span> <span class="token cString">"October"</span><span class="token cBase">,</span>
    <span class="token cNum">11</span><span class="token cBase">:</span> <span class="token cString">"November"</span><span class="token cBase">,</span>
    <span class="token cNum">12</span><span class="token cBase">:</span> <span class="token cString">"December"</span>
<span class="token cBase">}</span>
<span class="token cVar">print</span> switcher<span class="token cBase">.</span>get<span class="token cBase">(</span>argument<span class="token cBase">,</span> <span class="token cString">"Invalid month"</span><span class="token cBase">)</span></code></pre></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://github.ink/ramlaxman/Python-Interview-Questions/blob/master/Interview-Questions.md" rel="noreferrer" target="_blank" title=" Does Python have a switch-case statement? Interview Questions Source To Answer">github.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 10. What is pickling and unpickling?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.</p><ul><li><strong>Pickling</strong> - is the process whereby a Python object hierarchy is converted into a byte stream,</li><li><strong>Unpickling</strong> - is the inverse operation, whereby a byte stream is converted back into an object hierarchy.</li></ul></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://stackoverflow.com/questions/7501947/understanding-pickling-in-python" rel="noreferrer" target="_blank" title="What is pickling and unpickling? Interview Questions Source To Answer">stackoverflow.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 11. When to use a tuple vs list vs dictionary in Python?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><ul><li>Use a <code>tuple</code> to store a sequence of items that will not change.</li><li>Use a <code>list</code> to store a sequence of items that may change.</li><li>Use a <code>dictionary</code> when you want to associate pairs of two items.</li></ul></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://stackoverflow.com/questions/21389767/when-to-use-a-tuple-vs-list-vs-dictionary-in-python" rel="noreferrer" target="_blank" title="When to use a tuple vs list vs dictionary in Python? Interview Questions Source To Answer">stackoverflow.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 12. What is negative index in Python?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.</p></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://www.guru99.com/python-interview-questions-answers.html" rel="noreferrer" target="_blank" title="What is negative index in Python? Interview Questions Source To Answer">guru99.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 13. Suppose lst is 2, 33, 222, 14, 25, What is lst-1?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div class="mb-2"><span class="h5">Problem</span></div><div><div class="AnswerBody"><p>Suppose <code>lst</code> is <code>[2, 33, 222, 14, 25]</code>, What is <code>lst[-1]</code>?</p></div></div><div><div class="AnswerBody"><p>It's <code>25</code>. Negative numbers mean that you count from the right instead of the left. So, <code>lst[-1]</code> refers to the last element, <code>lst[-2]</code> is the second-last, and so on.</p></div></div><div class="row my-2"><div><span><i>Source:</i>&nbsp;<span><a href="https://adevait.com/python/interview-questions" rel="noreferrer" target="_blank" title="Suppose lst is [2, 33, 222, 14, 25], What is lst[-1]? Interview Questions Source To Answer">adevait.com</a></span></span>&nbsp; &nbsp;</div></div></div></div></div> <br><br></div><div data-v-5e9078c0="" class="unit"><div><h2> 14. How do you list the functions in a module?</h2></div> <div><h3>Answer:</h3> <div class="answer"><div><div><div class="AnswerBody"><p>Use the <code>dir()</code> method to list the functions in a module:</p><pre><code><span class="token cVar">import</span> some_module

print dir(some_module)

Related questions

0 votes
asked Jan 3 in Python by DavidAnderson
0 votes
asked Jan 3 in Python by DavidAnderson
...