Home > Net >  Why my parameter isn't callable for Python?
Why my parameter isn't callable for Python?

Time:09-17

so i'm trying to learn about classes in python but for some reason i can't get Wiek function working.

i get the error:

Traceback (most recent call last):
  File "D:/pythonProject/MEDIUM.py", line 36, in <module>
    fafik.Wiek()
TypeError: 'int' object is not callable
class Pies(Ssaki):
    def __init__(self, LiczbaKonczynPsa, ImiePsa, WiekPsa):
        self.LiczbaKonczyn = LiczbaKonczynPsa
        self.Wiek = WiekPsa
        self.Imie = ImiePsa

    def Przedstaw(self):
        print('Ten pies wabi się: ', self.Imie)
    def Konczyny(self):
        print('Ten pies posiada ', self.LiczbaKonczyn, 'kończyny')
    def Wiek(self):
        print('Wiek psa wynosi 6')

fafik = Pies(4, 'BOBO', WiekPsa=3)
fafik.Przedstaw()
fafik.Konczyny()
fafik.Wiek()

I'm sorry for asking maybe so stupid question but i truly can't fin solution to my problem.

CodePudding user response:

You're getting the error because you have both a class attribute named Wiek, which in your example is the int 3, and a method called Wiek(), which you try to call on the last line of your example. Since self.Wiek has already been defined as 3, calling fafik.Wiek() is the equivalent of calling fafik.3(), which is invalid.

CodePudding user response:

def Wiek(self):
        print('Wiek psa wynosi 6')
self.Wiek = WiekPsa

You have a parameter called Wiek, but also a method called Wiek When you call fafik.Wiek() python tries to call your parameter Wiek which is an int, and tries to call it as a function which is not possible since it's a int

In any case, don't give a parameter and a function the same name; except for very precise case (getters)

Just rename either the variable Wiek, or the function to a new name and it will work

CodePudding user response:

The problem you have here is that your class has two items having the same name.

  1. An attribute, of type 'int'.
  2. A method.

When you call your function here fafik.Wiek()it understands that you're trying to call the integer attribute as a method. Just change the name of your function.

  • Related