Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly use a function under a class?

I am currently learning Python. Since I am a big fan of OO (object-oriented) programming, obviously it's not hard to apply it in Python. But when I tried it, it seems very different to C#.

As you can see below, I am trying to create a character class, with three attributes Id, Hp, and Mana. The score is calculated by adding up Hp and Mana and then times 10.

As you can see, after defining MyChar where id=10 hp=100 mana=100, I was expecting MyChar.Score is (100+100)*10, which is 2000, but weirdly, it says:

bound method Character.Score of <__main__.Character object at 0x0000021B17DD1F60> as the result of print(MyChar.Score).

How can I fix this problem?

Here is my code:

class Character:

    def __init__(self, Id, Hp, Mana):
        self.Id = Id;
        self.Hp = Hp;
        self.Mana = Mana;


    def Score(self):
        return (self.Hp + self.Mana)*10;

MyChar = Character(10, 100, 100);

print(MyChar.Score)
like image 802
FrankW Avatar asked Jun 11 '19 11:06

FrankW


People also ask

How do you use a function inside class?

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.

When should a function be in a class?

Functions do specific things, classes are specific things. Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.

Can a function be defined in a class?

Functions can be declared and defined within the class definition.

What is a function that belongs to a class called?

Methods are functions that belongs to the class. There are two ways to define functions that belongs to a class: Inside class definition.


2 Answers

If you want to use it like a property in C#, decorate the function with @property, like so:

class Character:

    def __init__(self,Id,Hp,Mana):
        self.Id=Id;
        self.Hp=Hp;
        self.Mana=Mana;

    @property
    def Score(self):
        return (self.Hp+self.Mana)*10;

MyChar=Character(10,100,100);

print(MyChar.Score)

So you don't have to call it like a function.

For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property

like image 114
Adam.Er8 Avatar answered Oct 20 '22 00:10

Adam.Er8


tl;dr

Use it like any other function by calling it: print(MyChar.Score()) (note the additional pair of parentheses).


As you've correctly stated, MyChar.Score is a "function under a class" (aka "method"). So just use it like any other function by calling it: suffixing it with a pair of parentheses.

print(MyChar.Score())
#                 ^^

Without the call, simply doing print(MyChar.Score) prints <bound method blah blah>, i.e. the informal string representation of the method. The print function internally calls __str__() magic method (or __repr__(), if the former isn't defined). Hence, the following print equivalent lines:

print(MyChar.Score.__str__())
print(str(MyChar.Score))
print(MyChar.Score.__repr__())
print(repr(MyChar.Score))

In Python, functions are first-class citizens, hence they are objects and have the __str__() and __repr__() methods.

like image 40
TrebledJ Avatar answered Oct 19 '22 23:10

TrebledJ