Python Forum
Copying methods to effect the new owner instead of the old instance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copying methods to effect the new owner instead of the old instance
#1
Bug 
Here is my code, with a small debug loop...

class foo:
    def __init__(self, message="hello Damien", rotation=0):
        self.message = message
        self.rotation = rotation
        self.target_rotation = rotation

    def rotate(self):
        if self.rotation == 0:
            self.target_rotation = 30
        else:
            self.target_rotation = 0
    
    def great(self):
        print(self.message)

    def change_to(self, other):
        self.message = other.message
        self.rotation = other.rotation
        self.target_rotation = other.target_rotation
        
        self.ability1 = other.ability1
        self.ability2 = other.ability2

    def update(self):
        if self.rotation != self.target_rotation:
            if self.target_rotation == 30:
                self.rotation += 1
            else:
                self.rotation -= 1

    def ability1(self):
        pass
    def ability2(self):
        pass
            
class foo1(foo):
    def ability1(self):
        self.rotate()
    
    def ability2(self):
        self.change_to(foo2)


class foo2(foo):
    def ability1(self):
        self.rotate()
    
    def ability2(self):
        self.great()


foo1 = foo1()
foo2 = foo2(message="hello world")

while True:
    print("-----------")
    print("msg: " + str(foo1.message))
    print("rot: " + str(foo1.rotation))
    print("target_rot: " + str(foo1.target_rotation))
    inp = str(input("> "))
    if inp == "a":
        if foo1.rotation == 0:
            foo1.ability1()
        elif foo1.rotation == 30:
            foo1.ability2()
    # exit ask for input
    print(foo1.target_rotation)
    foo1.update()
So I create foo1 and foo2, then go throught the abilites of foo1, the second ability of foo1 is to change_to foo2 so it does, taking its arguments and its abilites(here is the question), as the ability of foo2 has self so it effects itself as an instance, running self.rotate as the new ability of foo1, will now change the target rotation of foo2 instance instead of foo1, so how can I while keeping this general structure make so when the abilites are copied, they now as self effect the instance instead of the other...

I didnt try much, asking AI, changing to import copy...
Reply


Messages In This Thread
Copying methods to effect the new owner instead of the old instance - by Daniel285 - Jun-02-2024, 02:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help seeking for prediction of fixed effect model gotodockforevildragon 1 249 Jun-23-2024, 08:09 AM
Last Post: johnthomson112
  Printing effect sizes for variables in an anova eyavuz21 2 1,132 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,322 May-23-2022, 09:44 PM
Last Post: Extra
  instance methods invokation mim 3 2,723 Apr-04-2021, 08:18 AM
Last Post: Gribouillis
  instance methods mim 7 3,443 Mar-28-2021, 03:46 PM
Last Post: deanhystad
  instance methods sharing addresses mim 1 2,377 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Rotation Effect on live Webcam Feed Leziiy 0 1,699 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  How to fix statement seems to have no effect hernancrespo89 0 7,050 Jan-23-2020, 09:23 PM
Last Post: hernancrespo89
  Copying a file Kundan 1 2,253 Aug-21-2019, 09:55 AM
Last Post: snippsat
  strange effect from duplicating a file descriptor Skaperen 1 2,154 Feb-18-2019, 08:20 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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