python - Why it's not possible to create object attribute outside object methods? -


When researching about Python class attributes and example attributes, I found out that it is not possible to exclude object attributes from objects. (

or class method can be). Like the code below, "NameError" will be generated in Python.

  class test (object): def __init __ (self): self.lst = [] self.str = 'xyz'   

Why the Python Do not allow? I am not questioning the language maker's decision but there is a reason behind this. As such, it is technically incorrect or any other disadvantage of this behavior.

You are defining a class, so there is no instance of pointing out external methods. Self:

  Drop square exam (object): def __init __ (self): self.lst = [] str = 'xyz'   

< Indicates code> self to example , not class. You need to either make an example and assign or directly to the attributes ( test (. Str = 'xyz' ), you will get a method (when Actually can actually refer to an example).

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -