Sorry this is my first time asking a question, my formatting may be wrong. I am unsure about the syntax for calling functions from a class without creating an instance of the class. For the code:
class A_Class:
var = 10
def __init__(self):
self.num = 12
def print_12(self):
return 12
How come I am able to call
print(A_Class.var)
And have the console print out the value 10, but if I were to call
print(A_Class.num)
Then I get the error:
AttributeError: type object 'A_Class' has no attribute 'num'
And if I try to call
print(A_Class.print_12)
Then the console prints:
<function A_Class.print_12 at 0x039966F0>
And not the value 12
I am confused with how to call functions from classes.
To call a function within class with Python, we call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self. distToPoint . self is variable storing the current Coordinates class instance.
The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class.
Answers (1) There are two ways to call another method function from the same class. First, you can use a dot/period to access the method from the class variable. Second, you can simply call the function and pass the class object as an argument.
In python, unlike other languages, you cannot perform method overloading by using the same method name. Why? Everything is an object in python, classes, and even methods. Say you have an object Addition, which is a class (everything in python is an object, so the class Addition is an object too).
var
is a Class
variable, while num
is an instance variable, as an example:
class A_Class:
var = 10
def __init__(self):
self.num = 12
def print_12(self):
return 12
a = A_Class()
As a class variable, it belongs to the class and you are able to call it.
print(A_Class.var)
>> 10
As an instance variable, you have to instantiate it before you can access the values, this is why self
(self
has no special meaning and can be anything, but always the first argument for instance methods) is used and is initialized in the special __init__
method.
a = A_Class()
print(a.num)
>> 12
Finally, you want to print the returned value, and therefore will have to call it such as:
var = a.print_12()
print(var)
>> 12
As you were missing the parenthesis earlier, it is the instance method itself, and therefore did not return any value.
To expand on @BernardL excellent answer about the differences between a class variable and an instance variable, I wish to add this is from the PyTricks newsletter I get which may help answer your question about print(A_Class.print_12)
.
# @classmethod vs @staticmethod vs "plain" methods
# What's the difference?
class MyClass:
def method(self):
"""
Instance methods need a class instance and
can access the instance through `self`.
"""
return 'instance method called', self
@classmethod
def classmethod(cls):
"""
Class methods don't need a class instance.
They can't access the instance (self) but
they have access to the class itself via `cls`.
"""
return 'class method called', cls
@staticmethod
def staticmethod():
"""
Static methods don't have access to `cls` or `self`.
They work like regular functions but belong to
the class's namespace.
"""
return 'static method called'
# All methods types can be
# called on a class instance:
>>> obj = MyClass()
>>> obj.method()
('instance method called', <MyClass instance at 0x1019381b8>)
>>> obj.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
>>> obj.staticmethod()
'static method called'
# Calling instance methods fails
# if we only have the class object:
>>> MyClass.classmethod()
('class method called', <class MyClass at 0x101a2f4c8>)
>>> MyClass.staticmethod()
'static method called'
>>> MyClass.method()
TypeError:
"unbound method method() must be called with MyClass "
"instance as first argument (got nothing instead)"
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