python - Cant understand dot operator concept -
People are really confused about the job of the dot operator. The code I have tried ..
class name: square x: def __init __ (self, y): self Y = y def __init __ (self): pass some = name () some.xy = 'foo' print something. Xy When I run this code, Code> foo .
But when I tried the code
class name: class x: def __init __ (self, y, z): self Y = y self.z = z def __init __ (self): pass some = name () some.xyz = 'foo' print some.xyz I get an error. How can i use it as some.xyz and get the output as foo ?? Any helpful help would be appreciated .. Thank you
It is not clear why you expected the second code to work. The first code is bad because it mixes classes and examples: some class Name is example ; some.x is an class , and class attribute 's name . If you create another name example, then it will share the same attributes at x . Instead, consider this: class name (object): def __init __ (self, x = none, y = none, z = none): self.x X = x (x = x) (Z = 'foo'))) # one-line equivalent print some.exe # 'afo' looks the result:
- Something: Name - x: name - x none - y: name - x: none - y: none - z: 'foo' - z: none - y: some No -z: none
the arbitrary chain of properties abcd and the Switch to expect it to work; You have to define the properties of your object (unless you get into sadness, but we do not worry about it anymore). You can not add voluntary attributes for example wire or none (so that you can no longer set some.xyzfoo = 'bar' ), although you can not set your classes In (so you Some.xybar = 'baz' ). However, you can not do this some.x.y.foo.bar = 'baz' , because some.x.y.foo is not present (yet).
Comments
Post a Comment