84

I have a class Person and a static method in that class called call_person:

class Person:
    def call_person():
        print "hello person"

In the python console I import the class Person and call Person.call_person(). But it is giving me error that says 'module' object has no attribute 'call_person'. Can anyone please let me know why I am getting this error?

Sadiksha Gautam
  • 5,032
  • 6
  • 40
  • 71

5 Answers5

179

You need to do something like:

class Person:
    @staticmethod
    def call_person():
        print("hello person")

# Calling static methods works on classes as well as instances of that class
Person.call_person()  # calling on class
p = Person()
p.call_person()       # calling on instance of class

Depending on what you want to do, a classmethod might be more appropriate:

class Person:
    @classmethod
    def call_person(cls):
        print("hello person", cls)

p = Person().call_person() # using classmethod on instance
Person.call_person()       # using classmethod on class

The difference here is that in the second example, the class itself is passed as the first argument to the method (as opposed to a regular method where the instance is the first argument, or a staticmethod which doesn't receive any additional arguments).

Now to answer your actual question. I'm betting that you aren't finding your method because you have put the class Person into a module Person.py.

Then:

import Person  # Person class is available as Person.Person
Person.Person.call_person() # this should work
Person.Person().call_person() # this should work as well

Alternatively, you might want to import the class Person from the module Person:

from Person import Person
Person.call_person()

This all gets a little confusing as to what is a module and what is a class. Typically, I try to avoid giving classes the same name as the module that they live in. However, this is apparently not looked down on too much as the datetime module in the standard library contains a datetime class.

Finally, it is worth pointing out that you don't need a class for this simple example:

# Person.py
def call_person():
    print("Hello person")

Now in another file, import it:

import Person
Person.call_person() # 'Hello person'
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    Is it more common in practice, with Python, to put utility functions which would generally get defined as a @staticmethod, into just a function within the module? – Thomas Farvour Jan 09 '14 at 18:24
  • 2
    @ThomasFarvour -- My rule of thumb is that *if* you use a staticmethod, you should be prepared to justify *why* it makes more sense as a staticmethod than a module level function. IMHO, the compelling usecases for staticmethods are pretty rare, but they do pop up once in a while. – mgilson Jan 10 '14 at 00:34
  • 4
    @mgilson Raymond Hettinger argues that making a method a ``staticmethod`` improves findability and helps to use it in the correct context. (https://youtu.be/HTLu2DFOdTg?t=31m21s) – Yurim Apr 08 '17 at 14:19
  • 1
    @Yurim -- That may be true, but Guido considers `staticmethod` to be something of a mistake (https://groups.google.com/forum/#!topic/python-ideas/McnoduGTsMw). So, I guess everyone should pick which core developer's philosophy they want to subscribe to ;-). As I said in my comment, I _do_ think there are cases where `staticmethod` are OK (see the comment by Brett Cannon on that thread), but I don't think that they are very wide-spread. That's _my_ opinion, and that's also why I'm relegating it to the comment section as opposed to the main body of the post :-) – mgilson Apr 09 '17 at 05:05
  • @mgilson Thanks for that link. – Yurim Apr 09 '17 at 20:47
  • Modern pylint complains if you inherit from object, as it's redundant. – Phil Jul 24 '20 at 03:52
  • Well, it was good advice when I wrote this back in 2012 ... Actually, it continued to be good advice for anything wanting to support python2.x until python2.7 support was finally dropped earlier this year... But you're right -- It really shouldn't be relevant in today's world. Updated to remove that advice. – mgilson Jul 26 '20 at 04:13
  • This answered many questions at once. – Shubham Srivastava Jun 07 '22 at 13:17
16

Everybody has already explained why this isn't a static method but I will explain why you aren't finding it. You are looking for the method in the module and not in the class so something like this would find it properly.

import person_module
person_module.Person.call_person() # Accessing the class from the module and then calling the method

Also as @DanielRoseman has said, you might have imagined that modules contain a class with the same name like Java although this is not the case in Python.

jamylak
  • 128,818
  • 30
  • 231
  • 230
8

In python 3.x, you can declare a static method as following:

class Person:
    def call_person():
        print "hello person"

but the method with first parameter as self will be treated as a class method:

def call_person(self):
    print "hello person"

In python 2.x, you must use a @staticmethod before the static method:

class Person:
    @staticmethod
    def call_person():
        print "hello person"

and you can also declare static method as:

class Person:
    @staticmethod
    def call_person(self):
        print "hello person"
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
shijq73
  • 709
  • 8
  • 6
5

That's not a static method; try

class Person:
    @staticmethod
    def call_person():
        print "hello person"

See here for more info.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 2
    In Python 3 the OP's definition **does** define a static method that you **can** invoke from the class object without the need for a class instance object. If anything not using the decorator gives you a method that is more "static" because you can't even call it from an instance object. When you use `@staticmethod` you **can** call the method from both the class and class instance objects. – Amelio Vazquez-Reina Apr 30 '20 at 02:02
0

You need to add the decorator classmethod.

Pran
  • 3,481
  • 2
  • 25
  • 27