I don't understand how python methods within a class work

Question:

So I’m new to programming and I’m trying to learn how to code in uni.

We just started learning classes in python and I can’t solve this problem.

I need to create a method reflect-x so that the variable y is negative.
Here is the code I wrote

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y

    def reflect_x(self):
        if self.y < 0:
            self.y = self.y
        else:
            self.y = -abs(self.y)
    def print(self):
        a = self.x , self.y
        print(tuple(a))

And this is what the site uses to check if the code is correct


p1 = Point(1, 4)
p1.reflect_x().print()

p2 = Point(-3, 5)
p2.reflect_x().print()

p3 = Point(-3, -5)
p3.reflect_x().print()

I keep getting " AttributeError: ‘NoneType’ object has no attribute ‘print’ " , and I don’t know what to do.

Asked By: FieldyScop

||

Answers:

You have to call two methods (reflect, print) separately.

But if you have instinct to do the in such way you can approach it like this

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y

    def reflect_x(self):
        if self.y > 0:
            self.y = -abs(self.y)
        return self # Return self will give you the instance itself

    def print(self):
        a = self.x , self.y
        print(tuple(a))

p1 = Point(1, 4)
p1.reflect_x().print()

p2 = Point(-3, 5)
p2.reflect_x().print()

p3 = Point(-3, -5)
p3.reflect_x().print()
Answered By: Rahul K P

Welcome to programming!

Your method defining reflect_x returns None, which doesn’t have a member function print. What you want is reflect_x to return a Point instead:

class Point:                                                                                            
    def __init__(self,x,y):                                                                             
        self.x=x                                                                                        
        self.y=y                                                                                        
                                                                                                        
    def reflect_x(self):                                                                                
        return Point(self.x, -self.y)                                                                   
                                                                                                        
    def printout(self):                                                                                 
        a = self.x , self.y                                                                             
        print(tuple(a))                                                                                 
                                                                                                        
p1 = Point(1, 4)                                                                                        
p1.printout()                                                                                           
                                                                                                        
p2 = Point(-3, 5)                                                                                       
p2.reflect_x().printout()                                                                               
                                                                                                        
p3 = Point(-3, -5)                                                                                      
p3.printout()                                                                                           
             

Note I changed print=>printout. Don’t use reserved keywords for names in your developing!!!

Answered By: JustLearning
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.