1

In Python is there any way to make a class, then make a second version of that class with identical dat,a but which can be changed, then reverted to be the same as the data in the original class?

So I would make a class with the numbers 1 to 5 as the data in it, then make a second class with the same names for sections (or very similar). Mess around with the numbers in the second class then with one function then reset them to be the same as in the first class.

The only alternative I've found is to make one aggravatingly long class with too many separate pieces of data in it to be readily usable.

1
  • Thanks for your answers, teifion gave the answer I was looking for on msn. My question wasn't clear. Basically though, "Is what you want basically a class that can reset an instance (object) to a set of default values? " yes it was.
    – leachrode
    Sep 15, 2008 at 16:06

6 Answers 6

5

A class is a template, it allows you to create a blueprint, you can then have multiple instances of a class each with different numbers, like so.

class dog(object):
    def __init__(self, height, width, lenght):
        self.height = height
        self.width = width
        self.length = length

    def revert(self):
        self.height = 1
        self.width = 2
        self.length = 3

dog1 = dog(5, 6, 7)
dog2 = dog(2, 3, 4)

dog1.revert()
2

Here's another answer kind of like pobk's; it uses the instance's dict to do the work of saving/resetting variables, but doesn't require you to specify the names of them in your code. You can call save() at any time to save the state of the instance and reset() to reset to that state.

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

    def save(self):
        self.saved = self.__dict__.copy()

    def reset(self):
        self.__dict__ = self.saved.copy()

a = MyReset(20, 30)
a.x = 50
print a.x
a.reset()
print a.x

Why do you want to do this? It might not be the best/only way.

1

Classes don't have values. Objects do. Is what you want basically a class that can reset an instance (object) to a set of default values?

How about just providing a reset method, that resets the properties of your object to whatever is the default?

I think you should simplify your question, or tell us what you really want to do. It's not at all clear.

1
  • 3
    Voted down: In Python, classes DO have attributes (values). Instances also have attributes (values). It's a very important distinction to understand when moving to python from another language.
    – gahooa
    Feb 18, 2009 at 18:39
1

I think you are confused. You should re-check the meaning of "class" and "instance".

I think you are trying to first declare a Instance of a certain Class, and then declare a instance of other Class, use the data from the first one, and then find a way to convert the data in the second instance and use it on the first instance...

I recommend that you use operator overloading to assign the data.

1
class ABC(self):
   numbers = [0,1,2,3]

class DEF(ABC):
   def __init__(self):
      self.new_numbers = super(ABC,self).numbers

   def setnums(self, numbers):
      self.new_numbers = numbers

   def getnums(self):
     return self.new_numbers

   def reset(self):
     __init__()
1

Just FYI, here's an alternate implementation... Probably violates about 15 million pythonic rules, but I publish it per information/observation:

class Resettable(object):
    base_dict = {}
    def reset(self):
            self.__dict__ = self.__class__.base_dict

    def __init__(self):
            self.__dict__ = self.__class__.base_dict.copy()

class SomeClass(Resettable):
    base_dict = {
            'number_one': 1,
            'number_two': 2,
            'number_three': 3,
            'number_four': 4,
            'number_five': 5,
    }
    def __init__(self):
            Resettable.__init__(self)


p = SomeClass()
p.number_one = 100
print p.number_one
p.reset()
print p.number_one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.