How can you organize your code to make it easier to change the base class?
You have to define an alias for the base class, assign the real base class to it before your class definition, and use the alias throughout your class. You can also use this method if you want to decide dynamically (e.g., depending on availability of resources) which base class to use.
Example
BaseAlias = <real base class>
class Derived(BaseAlias):
def meth(self):
BaseAlias.meth(self)
What are the applications of Python?
Python is used in various software domains some application areas are given below.
- Web and Internet Development
- Games
- Scientific and computational applications
- Language development
- Image processing and graphic design applications
- Enterprise and business applications development
- Operating systems
- GUI based desktop applications
Python provides various web frameworks to develop web applications. The popular python web frameworks are Django, Pyramid, Flask.
Python’s standard library supports for E-mail processing, FTP, IMAP, and other Internet protocols.
Python’s SciPy and NumPy helps in scientific and computational application development.
Python’s Tkinter library supports to create a desktop based GUI applications.
What is the namespace in Python?
The namespace is a fundamental idea to structure and organize the code that is more useful in large projects. However, it could be a bit difficult concept to grasp if you’re new to programming. Hence, we tried to make namespaces just a little easier to understand.
A namespace is defined as a simple system to control the names in a program. It ensures that names are unique and won’t lead to any conflict.
Also, Python implements namespaces in the form of dictionaries and maintains name-to-object mapping where names act as keys and the objects as values.
What is a negative index in Python?
Python sequences are accessible using an index in positive and negative numbers. For example, 0 is the first positive index, 1 is the second positive index and so on. For negative indexes -1 is the last negative index, -2 is the second last negative index and so on.
Index traverses from left to right and increases by one until end of the list.
Negative index traverse from right to left and iterate one by one till the start of the list. A negative index is used to traverse the elements into reverse order.
How to send an email in Python Language?
To send an email, Python provides smtplib and email modules. Import these modules into the created mail script and send mail by authenticating a user.
It has a method SMTP(smtp-server, port). It requires two parameters to establish SMTP connection.
A simple example to send an email is given below.
import smtplib
# Calling SMTP
s = smtplib.SMTP(‘smtp.gmail.com’, 587)
# TLS for network security
s.starttls()
# User email Authentication
s.login(“sender_email_id”, “sender_email_id_password”)
# message to be sent
message = “Message_you_need_to_send”
# sending the mail
s.sendmail(“sender_email_id”, “receiver_email_id”, message)
It will send an email to the receiver after authenticating sender username and password.
What is the difference between remove() function and del statement?
You can use the remove() function to delete a specific object in the list.
If you want to delete an object at a specific location (index) in the list, you can either use del or pop.
Note: You don’t need to import any extra module to use these functions for removing an element from the list.
We cannot use these methods with a tuple because the tuple is different from the list.
Which are the file related libraries/modules in Python?
The Python provides libraries/modules that enable you to manipulate text files and binary files on the file system. It helps to create files, update their contents, copy, and delete files. The libraries are os, os.path, and shutil.
Here, os and os.path – modules include a function for accessing the filesystem
while shutil – module enables you to copy and delete the files.
How Python does Compile-time and Run-time code checking?
In Python, some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user-defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.
What are iterators in Python?
In Python, iterators are used to iterate a group of elements, containers like a list. Iterators are the collection of items, and it can be a list, tuple, or a dictionary. Python iterator implements __itr__ and next() method to iterate the stored elements. In Python, we generally use loops to iterate over the collections (list, tuple).
What is the difference between list and tuple?
The difference between list and tuple is that a list is mutable while tuple is not.