August 26, 2018

Sreekanth B

Infosys Python Technical Interview Questions Answers 2017

Is there a tool to help find bugs or perform static analysis?

Yes. PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style. Pylint is another tool that checks if a module satisfies a coding standard, and also makes it possible to write plug-ins to add a custom feature.

How do you set a global variable in a function?

Did you do something like this?

x = 1 # make a global def f(): print x # try to print the global ... for j in range(100): if q>3: x=4 Any variable assigned in a function is local to that function. unless it is specifically declared global. Since a value is bound to x as the last statement of the function body, the compiler assumes that x is local. Consequently the print x attempts to print an uninitialized local variable and will trigger a NameError. The solution is to insert an explicit global declaration at the start of the function: def f(): global x print x # try to print the global ... for j in range(100): if q>3: x=4 In this case, all references to x are interpreted as references to the x from the module namespace.

How do I share global variables across modules?

Infosys Python Technical Interview Questions Answers 2017The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example: x = 0 # Default value of the 'x' configuration setting import config config.x = 1 import config import mod print config.x Note that using a module is also the basis for implementing the Singleton design pattern, for the same reason.

Is there an equivalent of C's "?

:" ternary operator?


What is a class?

A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embody both the data (attributes) and code (methods) specific to a datatype. A class can be based on one or more other classes, called its base class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined by inheritance. You might have a generic Mailbox class that provides basic accessor methods for a mailbox, and subclasses such as Mbox Mailbox, MaildirMailbox, Outlook Mailbox that handle various specific mailbox formats.

How do I avoid blocking in the connect() method of a socket?

The select module is commonly used to help with asynchronous I/O on sockets.

How do I generate random numbers in Python?

The standard module random implements a random number generator. Usage is simple: import random random.random() This returns a random floating point number in the range [0, 1).

Can I create my own functions in C?

Yes, you can create built-in modules containing functions, variables, exceptions and even new types in C.

Can I create my own functions in C++?

Yes, using the C compatibility features found in C++. Place extern "C" { ... } around the Python include files and put extern "C" before each function that is going to be called by the Python interpreter. Global or static C++ objects with constructors are probably not a good idea.

How do I emulate os.kill() in Windows?

Use win32api: def kill(pid): """kill function for Win32""" import win32api handle = win32api.OpenProcess(1, 0, pid) return (0 != win32api.TerminateProcess(handle, 0))

How can I find the methods or attributes of an object?

For an instance x of a user-defined class, dir(x) returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.

Subscribe to get more Posts :