serialization - Why would python `__setstate__` accept `None` as a parameter? -


I was browsing some code from and I in some serge.serialize Could not wrap around:

  def __setstate __ (self, state = none): "Start the object in the given state for self-reliance" "self.initial_properties = Bag () ## Get started first by default and then live state for this_state (self .__ class __._ getProperties (), state): If this_state: for name, value in i_State: setator (self S, name, value) setter.initial_properties, name, value)  

This method was derived from the serialjable object. I am confused about this because the state parameter is not the default value of __setet __ in any . Why will not an object be sent to __setet__ when unplick, and in what situation will it be useful?

This is not useful if your class is completely __getstate __ / Depending on snacks, the __settext___ is dependent.

Documents on Explaining:

On Uniquely, if the class has defined the __setset __ () , then it with the unauthorized state It is said ... if __getstate __ () returns an incorrect value, the code will not be called when the __ () method is opened.

Therefore, if your __getstate __ return none , you have the __setset_time time Will not be returned; You will not be called right now.

However, note that this is not true in classical classes. With a classic class, "if a class defines both __gatestate __ () and __setset __ () , the state object does not need to be a dictionary And what they can do in these ways. "(Actually, I believe there are cases where a false value turns into {} , which is not very well documented , But the same if statement handles it ...)

However, It is not understood why you need a default value ... If you can get any , then you have to type the code any ... But you do not need to write code that does not get the parameter exactly, right?

But, due to this you can do this.

First of all, note that the __setset___ of this particular class does more than usual: it first starts with "defaults and then living conditions". Therefore, this unit can be very easy to test that the default work is going to start.

On that, if you define a custom method and an underscore, then there is no reason why it does not have the same rules exactly as the default controller, or of course there is no reason to call it __setstate __ Calling on all - But if you say, build base class, that you expect your users to be sub-classes, as much as possible Work as much as possible, so that your sub The Ogkrta can instead __ setstate __ override to implement the reducing __ __ .

Similarly, if you keep your code as your own serializer at the top of Pickle / copyrights instead of using the exact rules As your code does not want to use the same rule, pickle Again, there is no reason that he has to use the same rules, but doing so may be very easy for your users to increase their classes.

None of these Surge , I have no information, but they are both things that can be easily applied to those types of types that you want to create.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -