Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I am learning Python and currently working with classes. I am trying to make a basic game to help learn it and am having a weird issue with calling methods from it. I have the main.py file which creates an instance from the class in the Character.py file.

This is the Character.py file:

class Character:  
    name=""  

    def __init__(Name):  
        name=Name  

    def getName():  
        return name

This is the main.py file:

from Character import *

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

I am not sure what the issue is. This is the error I get:

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

It is saying I am giving 1 positional argument but I don't see where I gave any. What am I missing?

like image 830
D. Stei Avatar asked Jun 01 '17 00:06

D. Stei


People also ask

How do you call a method inside a class Python?

Instance methods are built functions into the class definition of an object and require an instance of that class to be called. To call the method, you need to qualify function with self. . For example, in a class that contains functions first() and second(), first() can call second().

Can I call class method Python?

A class method is a method that's shared among all objects. To call a class method, put the class as the first argument. Class methods can be can be called from instances and from the class itself.

Can you call methods in __ init __?

Calling other methods from the __init__ methodWe can call other methods of the class from the __init__ method by using the self keyword. The above code will print the following output.


2 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, make sure to set the variable as an instance variable by using self, the 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 the new instance of a class as the first argument to all the class methods, like this in languages such as Java. The error comes from the fact that Python passes the instance as the first argument internally but your getter is not defined to take an argument.

With the above code, when you call the method upon an instance, the instance is internally passed as the first argument and Python doesn't complain as you specify that it takes an argument, self, and name is set correctly on the instance.


Note: By convention, Python does not use camelCase, but underscores, so your getter should by convention look like this:

def get_name(self):
    #...

Also see chepner's answer which explains why getters and setters aren't usually needed. Just get and modify the instance variable by using dot notation:

print(player.name) #get
player.name = "Jeff" #set
like image 182
Andrew Li Avatar answered Sep 21 '22 12:09

Andrew Li


As others have mentioned, even instance method must be declared with an extra argument, typically named self (although that is a conventional, not a required, 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 (such as public or private), so such trivial getters and setters aren't usually written. Documentation about which attributes you are "allowed" to modify are 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")
like image 22
chepner Avatar answered Sep 20 '22 12:09

chepner