python - Understanding property statement with class and instance variables -


so have read lot property keyword , believe have gotten gist of it. came across example

class peopleheight:     def __init__(self, height = 150):         self.height = height      def convert_to_inches(self):         return (self.height * 0.3937)      def get_height(self):         print("inside getter method")         return self._height      def set_height(self, value):         if value < 0:             raise valueerror("height cannot negative")         print("inside setter method")         self._height = value      height = property(get_height, set_height) #---->confusing 

and last statement confusing me

 height = property(get_height, set_height) 

i understand property statement has signature

property(fget=none, fset=none, fdel=none, doc=none) 

what dont get_height , set_height. understand height class variable (not instance variable) get_height , set_height.i thinking above should have been

height = property(get_height, set_height) 

but thats wrong syntax there no self.my question why dont error when say:

property(get_height, set_height) 

as there no definitions of get_height or set_height set class scope.

the statements in class body executed when class statement executed, if inside function. produces set of names, used set class attributes. see class statement documentation:

the class’s suite executed in new execution frame (see naming , binding), using newly created local namespace , original global namespace. (usually, suite contains function definitions.) when class’s suite finishes execution, execution frame discarded local namespace saved. [4] class object created using inheritance list base classes , saved local namespace attribute dictionary.

each of def ... statements produce function objects, assigned function name, get_height , set_height both just functions, end attributes on class. same applies height = property(get_height, set_height) line; accesses names get_height , set_height, calls property() callable these parameters , result assigned name height.

you may confused how methods , property object later on access instance (the self argument in methods). both functions , property objects descriptor objects; descriptor object accessed on instance , found on class automatically bound instance through protocol. see descriptor howto.

you can execute steps manually in interpreter:

>>> def get_height(self): ...     print("inside getter method") ...     return self._height ... >>> def set_height(self, value): ...     if value < 0: ...         raise valueerror("height cannot negative") ...     print("inside setter method") ...     self._height = value ... >>> height = property(get_height, set_height) >>> height <property object @ 0x105758188> >>> height.fget <function get_height @ 0x10560ce18> >>> height.fset <function set_height @ 0x10567e730> >>> class fakeheight: ...     _height = 42 ... >>> instance = fakeheight() >>> height.__get__(instance)  # descriptor access inside getter method 42 >>> height.__set__(instance, 82)  # descriptor setting inside setter method >>> instance._height 82 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -