'how's i can calling to int from def in class?

i have this code:

  class PLAYER:
    def player_move(self):
       if self.new_block == True:
           body_copy = self.body[:]
           body_copy.insert(0,body_copy[0] + self.direction)
           self.body = body_copy[:]
           self.new_block = False
           self.score += 1
   #print(self.score)

i want to calling self.score from outside the PLAYER class



Solution 1:[1]

You need to add a constructor (the init method) as done below, and within the constructor you must define self.score, as well as all the other fields you want your PLAYER class to have. In the code below I am assuming that when you are initializing a PLAYER object that you are providing the new_block, direction, and body information, however you defined these fields, which is unclear from the question.

class PLAYER:
    def __init__(self, new_block,direction,body):
        self.score = 0
        self.new_block = new_block
        self.direction = direction
        self.body = body
    
    def player_move(self):
       if self.new_block == True:
           body_copy = self.body[:]
           body_copy.insert(0,body_copy[0] + self.direction)
           self.body = body_copy[:]
           self.new_block = False
           self.score += 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jake Korman