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?
A static method is bound to the class and not the object of the class. Therefore, we can call it using the class name. A static method doesn't have access to the class and instance variables because it does not receive an implicit first argument like self and cls .
A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.
A static method has no access to the class as well as, for instance, variables since it does not get a starting argument such as cls and self. It is not possible to change the object's or class's state as an outcome. ClassName. method_name() and an object of the class can be used to call the class method.
Calling static methods If you create an object, we can call non-static methods. But you can also call the static method without creating the object.
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'
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.
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"
That's not a static method; try
class Person:
@staticmethod
def call_person():
print "hello person"
See here for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With