Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Python Interview Questions and Answers

Ques 11. How can my code discover the name of an object?

Ans. Generally speaking, it can't, because objects don't really have names. Essentially, assignment always binds a name to a value; The same is true of def and class statements, but in that case the value is a callable. Consider the following code:
class A:
pass
B = A
a = B()
b = a
print b
<__main__.A instance at 016D07CC>
print a
<__main__.A instance at 016D07CC>
Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance's name is a or b, since both names are bound to the same value.
Generally speaking it should not be necessary for your code to "know the names" of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.
In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question:
The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn't really care -- so the only way to find out what it's called is to ask all your neighbours (namespaces) if it's their cat (object)
....and don't be surprised if you'll find that it's known by many names, or no name at all!

Is it helpful? Add Comment View Comments
Ques 12. Is there an equivalent of C's "?:" ternary operator?
Ans. No.
Is it helpful? Add Comment View Comments
Ques 13. How do I convert a number to a string?
Ans. To convert, e.g., the number 144 to the string \'144\', use the built-in function str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct(). For fancy formatting, use the % operator on strings, e.g. \"%04d\" % 144 yields \'0144\' and \"%.3f\" % (1/3.0) yields \'0.333\'. See the library reference manual for details.
Is it helpful? Add Comment View Comments
Ques 14. How do I modify a string in place?
Ans. You can\'t, because strings are immutable. If you need an object with this ability, try converting the string to a list or use the array module:
>>> s = \"Hello, world\" >>> a = list(s)
>>>print a
[\'H\', \'e\', \'l\', \'l\', \'o\', \',\', \' \', \'w\', \'o\', \'r\', \'l\', \'d\'] >>> a[7:] = list(\"there!\")
>>>\'\'.join(a)
\'Hello, there!\'
>>> import array
>>> a = array.array(\'c\', s) >>> print a
array(\'c\', \'Hello, world\')
>>> a[0] = \'y\' ; print a
array(\'c\', \'yello world\')
>>> a.tostring()
\'yello, world\'
Is it helpful? Add Comment View Comments
Ques 15. How do I use strings to call functions/methods?
Ans. There are various techniques.
* The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:
def a():
pass
def b():
pass
dispatch = {\\\\\\\\\\\\\\\'go\\\\\\\\\\\\\\\': a, \\\\\\\\\\\\\\\'stop\\\\\\\\\\\\\\\': b} # Note lack of parens for funcs
dispatch[get_input()]() # Note trailing parens to call function
*
Use the built-in function getattr():
import foo
getattr(foo, \\\\\\\\\\\\\\\'bar\\\\\\\\\\\\\\\')()
Note that getattr() works on any object, including classes, class instances, modules, and so on. This is used in several places in the standard library, like this:
class Foo:
def do_foo(self):
def do_bar(self):
f = getattr(foo_instance, \\\\\\\\\\\\\\\'do_\\\\\\\\\\\\\\\' + opname)
f()
* Use locals() or eval() to resolve the function name:
def myFunc():
print \\\\\\\\\\\\\\\"hello\\\\\\\\\\\\\\\"
fname = \\\\\\\\\\\\\\\"myFunc\\\\\\\\\\\\\\\"
f = locals()[fname]
f()
f = eval(fname)
f()
Note: Using eval() is slow and dangerous. If you don\\\\\\\\\\\\\\\'t have absolute control over the contents of the string, someone could pass a string that resulted in an arbitrary function being executed.
Is there an equivalent to Perl\\\\\\\\\\\\\\\'s chomp() for removing trailing newlines from strings?
Starting with Python 2.2, you can use S.rstrip(\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\") to remove all occurences of any line terminator
from the end of the string S without removing other trailing whitespace. If the string S represents more
than one line, with several empty lines at the end, the line terminators for all the blank lines will be
removed:
>>> lines = (\\\\\\\\\\\\\\\"line 1 \\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\"
... \\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\"
... \\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\")
>>> lines.rstrip(\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\")
\\\\\\\\\\\\\\\"line 1 \\\\\\\\\\\\\\\"
Since this is typically only desired when reading text one line at a time, using S.rstrip() this way works
well.
For older versions of Python, There are two partial substitutes:
* If you want to remove all trailing whitespace, use the rstrip() method of string objects. This removes all trailing whitespace, not just a single newline.
* Otherwise, if there is only one line in the string S, use S.splitlines()[0].
Is there a scanf() or sscanf() equivalent?
Not as such.
For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the split() method of string objects and then convert decimal strings to numeric values using int() or float(). split() supports an optional \\\\\\\\\\\\\\\"sep\\\\\\\\\\\\\\\" parameter which is useful if the line uses something other than whitespace as a separator.
For more complicated input parsing, regular expressions more powerful than C\\\\\\\\\\\\\\\'s sscanf() and better suited for the task.
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2017 WithoutBook