Method in Python

Asked 2 years ago, Updated 2 years ago, 76 views

Hello.

When you create an object with Python, the line gets a little longer. The line is this long. Each object has an INS when it's turned.

Each individual has this long line of source code? There are times when I'm worried.

class puzzle(object):
    def __init__(self):
        self.puzzle8 = [[1,2,3],[4,5,6],[7,8,0]]
    def __getitem__(self,index):
        return self.puzzle8.__getitem__(index)
    def __setitem__(self,index,value):
        return self.puzzle8.__setitem__(index,value)
    def __str__(self):
        return str(self.puzzle8)
    def search(self,a):
        for i in range(0,3):
            for j in range(0,3):
                if a == self.puzzle8[i][j] :
                    return (i,j)
    def swap(self,a,b):
        self.puzzle8[a[0]][a[1]], self.puzzle8[b[0]][b[1]] = self.puzzle8[b[0]][b[1]], self.puzzle8[a[0]][a[1]]
    def left(self):
        point = self.search(0)
        if point[1] == 0 :
            return 'impossible'
        self.swap(point,(point[0],point[1]-1))
        print(self.puzzle8)
    def right(self):
        point = self.search(0)
        if point[1] == 3 :
            return 'impossible'
        self.swap(point,(point[0],point[1]+1))
        print(self.puzzle8)
    def up(self):
        point = self.search(0)
        if point[0] == 0 :
            return 'impossible'
        self.swap(point,(point[0]-1,point[1]))
        print(self.puzzle8)
    def down(self):
        point = self.search(0)
        if point[0] == 2 :
            return 'impossible'
        self.swap(point,(point[0]+1,point[1]))
        print(self.puzzle8)

For example, the above source code is about 40 lines. My question is, when you instantiate a class, each of those mesos is instantiated separately for each medium. Or just sharing a routine with a normal function? I'm curious about that.

To sum up. Does a method form only once when a class is instantiated, and then share one formed method?

I'd appreciate it if you could answer me._^

_^

python memory

2022-09-22 19:45

1 Answers

The questioner's question is not a line-by-line description.

Of course, if you write down the answer, you share the routine and use variables separately.

I recommend you to learn reverse engineering. All programs are binary code (assemblies) anyway.

If you enter parameters in the stack when calling a function in the assembly language and understand how the return address is pushed to the stack when calling the call opcode, you can understand the reason for using the local variable self or this.

Advanced language is not an environment for computers, I recommend you to learn assembly language.


2022-09-22 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.