Why can't I call a method from my Python class?

I am learning Python and am currently working with classes. I am trying to make a basic game to help learn it and I have a strange problem with method calls from it. I have a file main.py

that instantiates from a class in a file Character.py

.

This is the file Character.py

:

class Character:  
    name=""  

    def __init__(Name):  
        name=Name  

    def getName():  
        return name

      

This is the file main.py

:

from Character import *

player = Character("James")
print(player.getName())

      

I'm not sure what the problem is. This is the error I am getting:

Traceback (most recent call last):
  File "C:\Users\dstei\Documents\Python\It 102\Final Project\Main.py", line 
12, in <module>
    print(player.getName())
TypeError: getName() takes 0 positional arguments but 1 was given

      

They say that I am giving 1 positional argument, but I cannot see where I gave it. What am I missing?

+3


source to share


3 answers


Since you have a class with instance methods, you need to include the first argument ( self

by convention) to refer to the current instance. Also, don't forget to set the variable as an instance variable with the self

current instance:

class Character:  
    def __init__(self, Name): #self is the current instance
        self.name=Name  #set the variable on the instance so that every instance of Character has a name

    def getName(self):  
        return self.name #refer to the name with the instance

      

Python internally passes a new instance of the class as the first argument to all methods of the class, for example this

in languages ​​like Java. The error occurs because Python is passing an instance as the first argument internally, but your getter is not defined to accept the argument.

With the above code, when you call a method on an instance, the instance is internally passed as the first argument, and Python does not complain as you indicate that it takes an argument self

and name

set the instance correctly.




Note. By convention, Python doesn't use camelCase, but underscores, so your recipient should by convention look like this:

def get_name(self):
    #...

      

Also see chepner's answer which explains why getters and setters are usually unnecessary. Just inject and modify the instance variable using dot notation:

print(player.name) #get
player.name = "Jeff" #set

      

+8


source


As mentioned, even an instance method must be declared with an optional argument, usually called self

(although this is a common, not a mandatory name).

class Character:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        return name

      



However, Python does not have any kind of enforced visibility (like public or private), so such trivial getters and setters are usually not written. Documentation of what attributes you "allowed" to change is considered sufficient protection.

class Character:
    def __init__(self, name):
        self.name = name

c = Character("Bob")
print(c.name)  # instead of c.get_name()
c.name = "Charlie"  # instead of c.set_name("Charlie")

      

+4


source


You forget to add the self parameter. self

is an object reference to the object itself, so they are the same. Python methods are not called in the context of the object itself. self in Python can be used to handle custom object models, or

class Character:  

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

    def getName(self):  
        return self.name

      

To see why this parameter is needed, there are some good answers here:

What is the purpose of self-government?

0


source







All Articles