Python Forum
class definition and problem with a method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class definition and problem with a method
#1
Hi all,
I am trying to understand the correct usage of classes and methods (== functions inside the class definition?)
Below you see a class "Punkt", which refers to points in a 2D-plane with an x and y coordinate.
Simple enough, I try to add two points p1 and p2
First try: p1+p2 --> "unsupported operand type" (understood)
2nd try: Write a function "add" wich manipulates the .x and .y properties of the object "Punkt"
3rd try: Implement the function in the class definition (is that called "method"?) Here's the code:
class Punkt():
    def __init__(self, x = 99, y = 99):      #ALWAYS give default-values!
        self.x = x
        self.y = y

    def __str__(self):
        return f'( {self.x} | {self.y} )'
    
    def add(self, p1, p2):
        self = Punkt()             #WHY? WITH this line, every result is 99/99 
        self.x = p1.x + p2.x
        self.y = p1.y + p2.y
        return(self)
    
p1 = Punkt()                    #without __init (x and y defaults) here's an error
p1.x = 17                       #without __init (complete) no one has a problem
p1.y = 12

p2 = Punkt(-9,-4)

p3 = Punkt()
p3.add(p1,p2)                
print(f"Punkt 1: {p1}")
print(f"Punkt 2: {p2}")
print(f"Punkt 3: {p3}")
In line 10 you see me trying to initiate an object "Punkt" called "self", so I have something to work with.
But if I do so, the output of "print Punkt 3 {p3}" is always the default value of 99/99.
I thought, self = Punkt() calls an object called "self", which is a "Punkt()" and has the default values of x=99 and y=99.
After that, (line 11/12), I manipulate the self.x and self.y and return the new, edited values.

I tried much and over and over - just accidentially put a # in front of line 10 "self = Punkt()" and BOOM - it worked.

It would be very nice, if somebody could explain this to me (in simple words - kind of new to python :)

Greetings,
MrAyas

Output WITH line 10 active
Output:
Punkt 1: ( 17 | 12 ) Punkt 2: ( -9 | -4 ) Punkt 3: ( 99 | 99 )
Output WITHOUT line 10
Output:
Punkt 1: ( 17 | 12 ) Punkt 2: ( -9 | -4 ) Punkt 3: ( 8 | 8 )
Reply


Messages In This Thread
class definition and problem with a method - by HerrAyas - Apr-01-2024, 02:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 947 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  problem usage of static method akbarza 5 679 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  mutable argument in function definition akbarza 1 585 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  error occuring in definition a class akbarza 3 867 Nov-26-2023, 09:28 AM
Last Post: Yoriz
  determine parameter type in definition function akbarza 1 681 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  [split] Explain the python code in this definition Led_Zeppelin 1 808 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  Using one child class method in another child class garynewport 5 1,785 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Explain the python code in this definition Led_Zeppelin 1 1,179 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  meaning of -> syntax in function definition DrakeSoft 5 2,142 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,399 Mar-14-2022, 11:21 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020