Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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!!!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading