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.

Don Kirkby
56.7k28 gold badges225 silver badges309 bronze badges
asked Sep 15, 2008 at 15:51
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. Commented 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()
answered Sep 15, 2008 at 15:54
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Sep 17, 2008 at 13:09

Comments

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.

answered Sep 15, 2008 at 15:59

1 Comment

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

answered Sep 15, 2008 at 16:00

Comments

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__()
answered Sep 15, 2008 at 16:01

Comments

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
answered Sep 15, 2008 at 16:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.