Classes in Python

2019-06-07 06:48发布

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.

标签: python class
6条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-07 06:50

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.

查看更多
Bombasti
3楼-- · 2019-06-07 06:52

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
查看更多
Fickle 薄情
4楼-- · 2019-06-07 06:55

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.

查看更多
▲ chillily
5楼-- · 2019-06-07 06:55
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__()
查看更多
相关推荐>>
6楼-- · 2019-06-07 07:01

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-06-07 07:14

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()
查看更多
登录 后发表回答