by Arup Nanda
Questions
1. We have a simple class with just one attribute: empId
. Here is how we define the class and instantiate a variable called emp1
of that class. But it produced an error. Why?
# q1.txt
>>> class employee:
... def __init__ (self, empId):
... empId = self.empId
...
>>> emp1 = employee(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
AttributeError: 'employee' object has no attribute 'empId'
We have explicitly define an attribute named empId
; so why is it complaining that there is no attribute in that name?
2. What will be the output of the following:
#q2.txt
>>> class employee:
... def __init__ (self, empId):
... self.empId = empId
...
>>> emp1 = employee(1)
>>> emp1.__dict__['deptNo'] = 10
>>> print (emp1.empId + len(emp1.__dict__))
3. You want to define a function that doesn't accept any parameters. So, you write this:
def printMe:
But it produced en error. Why?
4. Consider the following code:
#q4.txt
def printMe():
print("In the function line 1")
print("In the function line 2")
print("About to call the function")
printMe()
print("After calling the function")
When you execute the program, will the output be as follows?
About to call the function
In the function line 1
In the function line 2
After calling the function
5. Here is a function to return the sum of two integer values:
#q5.txt
def mySum(p1,p2):
p3 = 0
print("Inside the function mySum")
return p3
p3 = p1 + p2
What will be output of the following?
print("About to call the function")
print('1 + 2 = ', mySum(1,2))
print("After calling the function")
6. What will be the result of the following code?
#q6.txt
myVar = 1
def myFunc():
myVar = myVar + 1
print('Inside function myVar=', myVar)
myVar = myVar + 1
myFunc()
print('Outside function myVar=', myVar)
7. Here is a program:
#q7.txt
def myMax(p1, p2, p3, p4):
print ("The maximum value is ", max(p1,p2,p3,p4))
myList = [5,2,7,1]
myMax(*myList)
What will be result? If it produces an error, explain where and why.
Answers
1. The error is due to the following:
empId = self.empId
It assigns self.empId
(which is the supposed attribute) to empId
, which, in this case, is considered a variable. The correct usage is the following:
self.empId = empId
2. The output will be 3. Here is the logic:
emp1.__dict__
will be a dictionary of all attributes of the instance emp1
of the class employee
. Initially we had only one attribute: empId
.- Then we added another attribute:
deptNo
. So len(emp.__dict__)
now will be 2. - The total length is 3.
3. The correct syntax is this:
def printMe():
You need to use the parentheses even if no parameters are expected.
4. The output will be different. Note line 3.
print("In the function line 2")
Line 2 is not indented. So, Python interprets it as outside the function. Therefore the output will be this:
In the function line 2
About to call the function
In the function line 1
After calling the function
5. It will not print the correct results. Note that the return
statement is the last statement in the function code. Other lines after that are not executed; but it never produces an error. Therefore, the line p3 = p1 + p2
is never executed. The function returns the value of p3
at that time, which is 0. The output will be this:
About to call the function
Inside the function mySum
1 + 2 = 0
After calling the function
6. The code will produce an error. Look at line myVar = myVar + 1
inside the function. Because it is inside the function, the scope of the variable myVar
is inside the function only. However, that variable has not been initialized before that call. The intent was probably to update the myVar
variable that is defined globally, but Python doesn't know that. If you want to update the global variable myVar
, simply add a line inside the function:
global myVar
Here is the complete updated code:
#q6a.txt
myVar = 1
def myFunc():
global myVar
myVar = myVar + 1
print('Inside function myVar=', myVar)
myVar = myVar + 1
myFunc()
print('Outside function myVar=', myVar)
Now the output is this:
Inside function myVar= 3
Outside function myVar= 3
Note how the variable has the same value inside and outside the function.
7. The code will not result in error. It will show "7." The input to the function is simply a list (*myList
), a list of four numbers was provided, and the function was expecting that many numbers. If you remove the *
in the call, the code would fail with the following error:
TypeError: myMax() missing 3 required positional arguments: 'p2', 'p3', and 'p4'
That's interesting but logical. If you didn't have the "*", mList
would be passed as a single parameter, which would happen to be of type list. The function was expecting four parameters, not one. Hence the error.
Back to the Part 4 article.
About the Author
Arup Nanda (arup@proligence.com has been an Oracle DBA since 1993, handling all aspects of database administration, from performance tuning to security and disaster recovery. He was Oracle Magazine's DBA of the Year in 2003 and received an Oracle Excellence Award for Technologist of the Year in 2012.