Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:59:53+00:00 2026-05-26T23:59:53+00:00

The class Event implements a function copyFrom(self, event) and copy(self) . The implementation of

  • 0

The class Event implements a function copyFrom(self, event) and copy(self). The implementation of both methods is simple made cake.

class Event(object):

    def __init__(self, a, b):
        self.a = a
        self.b = b

    def copyFrom(self, event):
        self.a = event.a
        self.b = event.b

    def copy(self):
        return Event(self.a, self.b)

Now, the subclass MouseEvent wants to override both of them. The first method, copyFrom(self, event) can be implemented by calling the super-method.

class MouseEvent(Event):

    def __init__(self, a, b, c, d):
        super(Event, self).__init__(a, b)
        self.c = c
        self.d = d

    def copyFrom(self, event):
        super(Event, self).copyFrom(event)
        self.c = event.c
        self.d = event.d

But what about copy(self) ? Of course, just creating a new object is not hard.

    # ...
    def copy(self):
        return MouseEvent(self.a, self.b, self.c, self.d)

But what if the Event class owns some private attributes ? The person that wants to subclass it, does not want to care about them !

class Event(object):

    def __init__(self, a, b):
        self.a = a
        self.b = b
        self._aab = fancyFunction(a, b)

    def doStuff(self, c):
        self._aab <<= c * 2

    def copyFrom(self, event):
        self.a = event.a
        self.b = event.b
        self._aab = event._aab

    def copy(self):
        e = Event(self.a, self.b)
        e._aab = self._aab
        return e

This implementation of copy(self) looks quite dirty now, and so does the overriden one from MouseEvent.

class MouseEvent(Event):

    # ...
    def copy(self):
        e = MouseEvent(self.a, self.b, self.c, self.d)
        e._aab = self._aab
        return e

How can I implement an easy, maybe recursive, copy-behaviour in this particular example ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:59:53+00:00Added an answer on May 26, 2026 at 11:59 pm

    First, isn’t it enough that the MouseEvent constructor recursively calls the Event constructor?

    If not, have a look at the copy module. It is based on pickle‘s state retrieval and restoration protocol. Using this protocol, recursive calls are easy. You could of course just copy this basic design if you don’t want to conform to the pickle interface, by I don’t see any advantage to doing so.

    Edit: It seems I failed to get across the message, and indeed my answer might be a bit vague. So here we go with a code example using the suggested design:

    class Event(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b
            self._aab = fancyFunction(a, b)
        def retrieve_state(self):
            return self.a, self.b, self._aab
        def restore_state(self, state):
            self.a, self.b, self._aab = state
        def copy_from(self, event):
            self.restore_state(event.retrieve_state())
        def copy(self):
            e = object.__new__(self.__class__)
            e.restore_state(self.retrieve_state())
            return e
    
    class MouseEvent(Event):    
        def __init__(self, a, b, c, d):
            Event.__init__(self, a, b)
            self.c = c
            self.d = d
        def retrieve_state(self):
            event_state = super(MouseEvent, self).retrieve_state()
            return event_state, self.c, self.d
        def restore_state(self, state):
            event_state, self.c, self.d = state
            super(MouseEvent, self).restore_state(event_state)
    

    Note that you wouldn’t even need the copy() and copy_from() methods if you would simply rename retrieve_state() and restore_state() to __getstate__() and __setstate__(), respectively, because after toding this you can just use the standard copy() module. (And in the case at hand, you wouldn’t even need to define __getstate__() and __setstate__() at all, since the default behaviour is to save and restore all instance attributes.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Foo(){ public List<string> SomeCollection; } I need to implement an event which can
Implemented the Sink Class - to receive event notifications from COM Server Event Interface
I have defined an Event class: Event and all the following classes inherit from
im trying to do this: class Event < ActiveRecord::Base belongs_to :previous_event has_one :event, :as
Let's say I have defined the following class: public abstract class Event { public
I have following event class. I have a question related to the Property method
I have an Event class. Due to the way dates are handled, we need
Take the following C# class: c1 { event EventHandler someEvent; } If there are
I need to avoid serializing an Event class member because when the event is
I will use (again) the following class hierarchy: Event and all the following classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.