Thursday, 15 May 2014

python - Class retains previous content where new instance is expected -


I define a square, and a function that creates an example of that class I thought that this function Every time a new example should be made, however, it seems that it "Inherits" its content from the previous call. Can anyone explain this? Thanks! Class test: a = [] def B (self, x): self.a.append (x) def add (): t = test () TB (2) returns a print Plus (). A

Output:

  [2] [2, 2] [2, 2, 2]    

Here is how the definition of a example variable should be shown:

  Class test (object): def __init __ (self): self.a = []   

The way it was before a is the form of frequency variable , But a class variable which was being shared in all instances of the class.

No comments:

Post a Comment