Olivier Turcotte Olivier Turcotte - 1 month ago 7
Python Question

PYTHON - Error while calling a outter function

This is the code where i created my function

#============= Importations =====================
from datetime import date

#============= Classe ===========================
class Pret :

#----------- Constructeur -----------------------------

def __init__(self,p_date:str,p_duree_max:int,p_nbr_renouv):
#.... Déclaration des données membres ....
self.__date_debut = None # date
self.__duree_max = 0 # int
self.__nbr_renouv = 0 # int
#.... Code ....
self.__date_debut = p_date
self.__duree_max = p_duree_max
self.__nbr_renouv = p_nbr_renouv

#----------- Autres méthodes -------------------------
def calculer_date_retour():
debut=self.__date_debut
debut_a_auj= debut.toordinal()
date_retour=fromordinal(debut_a_auj+self.__duree_max)
return date_retour


The "calculer_date_retour" function should give me a date from "date_debut" and "duree_max" who's are given from this program :

#----------- Importations et fonctions ----------------
from datetime import date
from pret import Pret
#----------- Déclaration des variables ----------------
debut = None
dates = None
duree = 0
nbr_renouv = 0
compte_a = None
x=0
#----------- Code -----------
debut=date(2000,1,10)
print(debut)
dates = date.today()
duree = 10
nbr_renouv = 2


compte_a=Pret(dates, duree, nbr_renouv)
print(compte_a.calculer_date_retour())


but when i try the
print(compte_a.calculer_date_retour)
it give me error like this:

Traceback (most recent call last):
File "Z:Fichier de test.py", line 25, in <module>
print(compte_a.calculer_date_retour())
TypeError: calculer_date_retour() takes 0 positional arguments but 1 was given


basicly it say that i give an argument to it but i cant figure out where am i giving him one

Answer

When calling a function from a class, self is always given as a default argument, this means you need self as a parameter when definining the function

def calculer_date_retour(self):
    debut=self.__date_debut
    debut_a_auj= debut.toordinal()
    date_retour=date.fromordinal(debut_a_auj+self.__duree_max)
    return date_retour