-1

I have this code, but when I try to use the re-defined str function it doesn´t print anything and, more important, the defined function named "cuadrant" doesn´t work as it´s not defined according to python, but it is:

class Point:
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y
        print('The point ({},{}) has been created'.format(self.x, self.y))

    def __del__(self):
        print('The point ({},{}) has been deleted or overwritten'.format(self.x, self.y))

    def __str__(self):
        return '({},{})'.format(self.x, self.y)

    def cuadrant(self):
        if self.x > 0 and self.y > 0:
            return '1st Cuadrant'
        elif self.x < 0 and self.y > 0:
            return '2nd Cuadrant'
        elif self.x < 0 and self.y < 0:
            return '3rd Cuadrant'
        elif self.x >0 and self.y < 0:
            return '4th Cuadrant'
        else:
            return 'Original Point'

A = Point(4, 5)
B = Point(8, 9)

str(A)
str(B)

cuadrant(A)
cuadrant(B)

The error message I get is this one:

runfile('C:/Users/Jorge87/Desktop/Master Tema 1.5/prueba.py', wdir='C:/Users/Jorge87/Desktop/Master 
Tema 1.5')
The point (4,5) has been created
The point (8,9) has been created
Traceback (most recent call last):

File "<ipython-input-9-16e5df591861>", line 1, in <module>
runfile('C:/Users/Jorge87/Desktop/Master Tema 1.5/prueba.py', wdir='C:/Users/Jorge87/Desktop/Master 
Tema 1.5')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 
827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 
110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Jorge87/Desktop/Master Tema 1.5/prueba.py", line 38, in <module>
cuadrant(A)

NameError: name 'cuadrant' is not defined
JMarcos87
  • 183
  • 3
  • 11

2 Answers2

1

It seems like you are misunderstanding the concept of a method. in python, a method is called from the object using a dot, namely A.cuadrant() -- Good, cuadrant(A) -- Bad.

Edit #1: I should mention that in some languages, for example c++, it is possible to call a method using the namespace of the class and passing a pointer to the object as the first parameter

Edit #2: Inspired from the comments and edit #1, Point.cuadrant(A) might work, try it too. try different methods of executing methods in order to fully understand how they work.

Lior Yehezkely
  • 101
  • 1
  • 10
1

Question 1. re-defined str function it doesn´t print anything

You use str in your code:

str(A)
str(B)

str returns your expected string, but you are doing nothing with it.
So nothing is printed. You should do:

print(str(A))
print(str(B))

Question 2. "cuadrant" doesn´t work

Methods can be reference by class's namespace.
So if you want to refer cuadrant method, you should code Point.cuadrant.

print(Point.cuadrant(A))

might work.
Or, more normally:

print(A.cuadrant())
Boseong Choi
  • 2,566
  • 9
  • 22