1

I am studying classes in python programming in jupyter notebooks and google colab.

I don't understand the results with respect to this class.

class employee_constructor():

  def __init__(self,name,surname,salary):
   self.name=name
   self.surname=surname
   self.salary=salary

  def increasesalary(self,percentage):
    self.salary=self.salary*(1+percentage/100)

  def displayEmployee(self):
     print('this employee is {} and gets {} dollars'.format(emp1.name,emp1.salary))

now I try to print out results:

emp1=employee_constructor('jose','ferro',1000)
emp2=employee_constructor('manolo','rod','1500')
emp1.displayEmployee
print('before increase',emp1.salary)
emp1.increasesalary(5)
emp1.increasesalary(5)
print('after increase',emp1.salary)

print(emp1.salary)

# this line does not give error and does nothing:
emp1.increasesalary
print(emp1.salary)

# this line gives error:
# increasesalary() missing 1 required positional argument: 'percentage'
emp1.increasesalary()

I don't understand why running the method without the parenthesis would not cause any error (actually the method is not run) whereas with the parenthesis (and not passing the neccesary variable through an error)

secondly, how can I avoid such kind of errors? i.e. if the user passes nothing assume vale zero

note: this question explains init method and was proposed as solution. My question is related but is not answered there

JFerro
  • 3,203
  • 7
  • 35
  • 88

2 Answers2

3

I don't understand why running the method without the parenthesis would not cause any error (actually the method is not run) whereas with the parenthesis (and not passing the neccesary variable through an error)

When you refer a method (function in the context of an object, self is passed implicitly) by object.method the method object is returned. But to actually execute the function you need to call it i.e. use the parentheses.

For fun, save the returned method object as a variable and call that instead, you'll see that you're doing the same thing as they refer to the same object.

Now, when you called emp1.increasesalary(), you didn't pass the required argument percentage leading to the error. Note again, the self (object itself) is passed implicitly.

how can I avoid such kind of errors? i.e. if the user passes nothing assume vale zero

Make the argument a keyword argument with a default value of 0:

def increasesalary(self, percentage=0):
    self.salary = self.salary * (1 + percentage / 100)
heemayl
  • 39,294
  • 7
  • 70
  • 76
1

you can always use a funtion (without parenthesis) in python:

def f():
    pass

print(f)

this will not call the function but just print out its memory location. so a line containing the function f itself is a valid python statement; but it does not call the function.


then: you need to use self and not emp1 in your displayEmployee(self) method:

def displayEmployee(self):
    print('this employee is {} and gets {} dollars'.format(self.name, self.salary))

better:

def __str__(self):
    return f"this employee is {self.name} and gets {self.salary} dollars"

then you can

print(emp1)
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111