5

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.

Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52
  • Hi Nolan and welcome to SO! If any of the answers here have solved your question, kindly accept the best one as an answer by ticking the checkmark below the vote buttons next to the answer. – BernardL Dec 03 '18 at 03:41

3 Answers3

4

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.

BernardL
  • 5,162
  • 7
  • 28
  • 47
2

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)"
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
0

This is because what you define in the class root level is a static variable or method.

Also the methods within the class are objects themselfs, so if you print them this returns the object type and memory address as there is not way defined to print (or convert to string) the object (normally specified with __str__ otherwise).

b-fg
  • 3,959
  • 2
  • 28
  • 44