Metamorphosis of the attributes of the class

 3r33150. 3r3-31. A short note from the “You Warned” series
 3r33150. The transition from classical programming languages ​​to Python delivers a lot of surprises. 3r3138.  3r33150. We read documentation:
 3r33150.
For example, it is important to say that
3r3138.  3r33150. 3r3138.  3r33150. Let's try to play
 3r33150. 3r3122. class Vessel:
#class attribute
vtype = "boat"
3r33150. #instance attribute
def __init __ (self, name):
self.name = name
3r33130.
3r3138.  3r33150.
3r330. 3r3138.  3r33150. Create two objects and check the values ​​of all attributes: 3r3138.  3r33150. 3r3122. Iowa = Vessel ("Iowa") 3r3-33150. Drum = Vessel ("Drum") 3r3-3150. printAttr (Iowa, Drum) 3-333150. 3r33150. name = Iowa vtype = boat __class __. vtype = boat
name = Drum vtype = boat __class __. vtype = boat
3r33130.
3r3138.  3r33150. So far, everything is as expected. 3r3138.  3r33150. We will try to change the vtype: this can be done in two ways, which are essentially just a different syntax of the same 3r3138.  3r33150. 3r3122. Vessel.vtype = "USS boat" 3r3-3150. printAttr (Iowa, Drum) 3-333150. name = Iowa vtype = USS boat __class __. vtype = USS boat
name = Drum vtype = USS boat __class __. vtype = USS boat 3r3-3150. 3r33150. Iowa .__ class __. Vtype = 'USS WW2 Boat' 3r-31b. printAttr (Iowa, Drum) 3-333150. name = Iowa vtype = USS WW2 Boat __class __. vtype = USS WW2 Boat
name = Drum vtype = USS WW2 Boat __class __. vtype = USS WW2 Boat
3r33130.
3r3138.  3r33150. And again, everything is in order. 3r3138.  3r33150. Now we will try to do the same through the object attribute. 3r3138.  3r33150. 3r3122. Drum.vtype = 'submarine' 3r3-3150. printAttr (Iowa, Drum) 3-333150. name = Iowa vtype = USS WW2 Boat __class __. vtype = USS WW2 Boat
name = Drum vtype = submarine __class __. vtype = USS WW2 Boat
3r33130.
3r3138.  3r33150. And here is the first surprise: despite the fact that vtype is an attribute of a class, it suddenly becomes an attribute of an object. 3r3138.  3r33150. Check: 3r3138.  3r33150. 3r3122. Vessel.vtype = "NAVY Museum"
name = Iowa vtype = NAVY Museum __class __. vtype = NAVY Museum
name = Drum vtype = submarine __class __. vtype = NAVY Museum
3r33130.
3r3138.  3r33150. and what if 3r3138.  3r33150. 3r3122. del Drum.vtype
name = Iowa vtype = NAVY Museum __class __. vtype = NAVY Museum
name = Drum vtype = NAVY Museum __class __. vtype = NAVY Museum
3r33130.
3r3138.  3r33150. And again class attribute. 3r3138.  3r33150. The following expression no longer passes
 3r33150. 3r3122. del Drum.vtype
printAttr (Iowa, Drum) 3-333150. del Drum.vtype
AttributeError: vtype
3r33130.
3r3138.  3r33150. And the last example that emulates class overrides and the removal of the vtype attribute. 3r3138.  3r33150. 3r3122. Drum.vtype = 'submarine' 3r3-3150. del Vessel.vtype
printAttr (Iowa, Drum) 3-333150. 3r33150. name = Iowa
name = Drum vtype = submarine
3r33130.
3r3138.  3r33150. 3r3138.  3r33150. If you start to deal with namespace-s, then this behavior becomes clear. 3r3138.  3r33150. However, for programmers who have previously worked with normal languages, this at least seems strange. And if we talk about large projects that are supported by several generations of developers, this may turn out to be a deadline, etc. 3r3138.  3r33150. Taking into account the concept of Python, that everything is open to all, why not make access to the "cool" attributes only through __class__ or its equivalent. In my opinion, it would somehow protect against surprises and make you think 10 times before assigning something to cool attributes at the object level. 3r3146. 3r33150. 3r33150. 3r3143. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r3144. 3r33150. 3r3146. 3r33150. 3r33150. 3r33150. 3r33150.
+ 0 -

Add comment