Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wanna Answers of PYTHON EXPERTS and please if you cant do it fully dont post a

ID: 3876765 • Letter: I

Question

I wanna Answers of PYTHON EXPERTS and please if you cant do it fully dont post anything

Define what a reference to an object means in the context of Python programming and explain what using a reference entails. You may wish to provide examples to make your point clearer, but make sure you describe the idea first. So This question was also about the fundamental concept of programming, regarding references to objects/variable Which object-oriented programming concept is employed as a code reuse mechanism? Name this concept and describe it. This question did NOT ask for the explanation of what code reuse is. It asked about the name of the OOP concept that provides for. codeand how that concept allows for code reuse. Explain what a decorator is in the context of Python. Describe at least one advantageof using decorators. At least two advantage not one Explain what the singleton pattern is. Explain if this pattern is necessary in the context of Python object-oriented programming and how it may be implemented in Python.

Explanation / Answer

(1) Answer

Python variables reference objects which contain the value, type information and reference counter.When we say reference a variable just points to the object which is only a block of memory that contains information. When a variable points to an object that connection between the two is referred to as a reference.

we are going to look at how the actual reference works. Let's dive into how Python variables reference objects.

Python Reference With One Variable and One Object

a = 2

In this case, the variable would point to the object. The variable is "a" and the object is "2". So, "a" points to the object that contains "2".

Python Reference With Two Variables Same Object

a = 2 , b = 2

Here we have two variables with the same value. In this case, Python would use the same object since "2" is the same for both variables. Have a look at another diagram how Python would reference the objects.

Two Python Variables One Object In the above case, you can see both variables are referencing the same object even tho they are different variables. Variable Assigned To Another Variable

a = 2

b = a

In this case, we set a = 2 and then we assign the variable "b" to the variable "a". In this case the variable "b" does not actually reference "a". "b" references "a's" object not the variable. We will use the same diagram from above since it is actually the same.

(2)Answer

One of the major benefits of object oriented programming is reuse of code and one of the ways this is achieved is through the inheritance mechanism

Suppose you want to write a program which has to keep track of the teachers and students in a college. They have some common characteristics such as name, age and address. They also have specific characteristics such as salary, courses and leaves for teachers and, marks and fees for students.

You can create two independent classes for each type and process them but adding a new common characteristic would mean adding to both of these independent classes. This quickly becomes unwieldy.

A better way would be to create a common class called SchoolMember and then have the teacher and student classes inherit from this class, i.e. they will become sub-types of this type (class) and then we can add specific characteristics to these sub-types.

The SchoolMember class in this situation is known as the base class or the superclass. The Teacherand Student classes are called the derived classes or subclasses.

Output:

How Programme Works Explaination

To use inheritance, we specify the base class names in a tuple following the class name in the class definition (for example, class Teacher(SchoolMember)). Next, we observe that the __init__ method of the base class is explicitly called using the self variable so that we can initialize the base class part of an instance in the subclass. This is very important to remember- Since we are defining a __init__method in Teacher and Student subclasses, Python does not automatically call the constructor of the base class SchoolMember, you have to explicitly call it yourself.

In contrast, if we have not defined an __init__ method in a subclass, Python will call the constructor of the base class automatically.

While we could treat instances of Teacher or Student as we would an instance of SchoolMember and access the tell method of SchoolMember by simply typing Teacher.tell or Student.tell, we instead define another tell method in each subclass (using the tell method of SchoolMember for part of it) to tailor it for that subclass. Because we have done this, when we write Teacher.tell Python uses the tell method for that subclass vs the superclass. However, if we did not have a tell method in the subclass, Python would use the tell method in the superclass. Python always starts looking for methods in the actual subclass type first, and if it doesn’t find anything, it starts looking at the methods in the subclass’s base classes, one by one in the order they are specified in the tuple (here we only have 1 base class, but you can have multiple base classes) in the class definition

(3) Answer

Python has an interesting feature called decorators to add functionality to an existing code.This is also called metaprogramming as a part of the program tries to modify another part of the program at compile time

Example First:

Output:

Something is happening before some_function() is called.

To understand what’s going on here, just look back at the four previous examples. We are literally just applying everything learned. Put simply, decorators wrap a function, modifying its behavior

Example Second

Output:

Yes!

Wheee!

(4) Answer

Singleton pattern provides a mechanism to limit the number of the instances of the class to one. Thus the same object is always shared by different parts of the code. Singleton can be seen as a more elegant solution to global variable because actual data is hidden behind Singleton class interface. First we look at how Singleton can be implemented in C++ and then provide a Python solution with the same features.

Because Python has no private constructors we have to find some other way to prevent instantiations. Our approach in Figure 3 is to raise an exception if the Singleton object is already instantiated (private class attribute __single is other than None).

Example:-

  class Singleton:      __single = None      def __init__( self ):          if Singleton.__single:              raise Singleton.__single          Singleton.__single = self   
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote