[Code has the Truth]python class private properties and other points to understand and test the sample code

Source: Internet
Author: User

# encoding: utf-8 ' 1. python the property of "__" at the beginning of the contract class definition is "relative" to the private property 2.  the private property is "visible" in the class and its subclass definition,   Other range normal access is relatively "invisible" 3.  left subclasses can modify the private properties of the parent class of the hole convenient debug test and other 4.  in the class definition can be accessed through the [' _ ' +classname+ ' private property name '] name Access 5.   Private attribute introduction is to avoid confusion and conflicts such as classes, instances, etc., private properties are constrained by everyone's "statute", so it is relative to the 6.  class definition similar to "class. Private property" related operations can be approximated as class "new" corresponding class properties, and in the context of "valid" 7.   The "valid" private property has a direct effect on the corresponding attributes of class and subclass instances in the subsequent same context 8.  note that subclasses override the parent class for non-private properties and subsequent subclass properties when the class "new" property is outside the class definition Created on  2015 September 4 @author: laughlast "' Import unittest2_int_direct_changed_to = 3333_int_ changed_by_instance = 3000_cls_private_attr =  ' __private_cls_attr ' _CLS_NON_PRIVATE_ATTR  =  ' Non_private_cls_attr ' class sample (object):     ' Simple class  with class attribute '     # private attribute     __private_cls_attr = 3    # non private attribute     non_private_cls_attr = 3    def __init__ (self):         self.init_attribute_x = 1        self.init_attribure_y  = 1    def add (self):         return  self.init_attribute_x + self.init_attribure_y      @classmethod      def get__private_cls_attr (CLS):         return  cls.__private_cls_attr     @classmethod     def get_non_ Private_cls_attr (CLS):         return cls.non_private_cls_attr      @classmethod     def initvalue (CLS, P, NP):         cls.__private_cls_attr = p         cls.non_privatE_cls_attr = npclass samplesubcls (Sample):     # private attribute     __private_cls_attr = 33    # non private  attribute    non_private_cls_attr = 33class  Classattributeprivateornotdifference (Unittest2. TestCase):     def setup (self):         "make  sure the same context of critical attr values          "        sample.initvalue (3, 3)          samplesubcls.initvalue (33, 33)          assert sample.get__private_cls_attr ()  is 3              and sample.get_non_private_cls_attr ()  is 3  &nbSp;      assert samplesubcls.get__private_cls_attr ()  is 33              and samplesubcls.get_non_private_ Cls_attr ()  is 33    def test_private_attr_definition_convention (self):          ' actual complex name of private  Attr with in cls '         internal__class_int =   ' _ '  + Sample.__name__ + _CLS_PRIVATE_ATTR         assert hasattr (Sample, internal__class_int)  is True         assert getattr (Sample, internal__class_int)  is sample.get__private_cls_ attr ()         internal__subclass_int =  ' _ '  +  Samplesubcls.__name__ +&nbSp;_cls_private_attr        assert hasattr (SampleSubCls,  Internal__subclass_int)  is true        assert getattr ( Samplesubcls, internal__subclass_int)  is samplesubcls.get__private_cls_attr ()      def test_hasattr_diff (self):         "cls private  attr sees as  "Invisible"  out of its cls definition         ref: hasattr (cls,private_attr)  return False          '         assert hasattr ( SAMPLE, _CLS_PRIVATE_ATTR)  is False              and hasattr (sample, _cls_non_private_attr)  is True         aSsert hasattr (samplesubcls, _cls_private_attr)  is False             and hasattr (samplesubcls, _cls_non_private_attr)  is  true    def test_class_dotted_assignment_effects_on_class (self):          ' Cls dotted assignment to private attr out  of cls definition        sees as a  "New " attr of the running context away from original         notice the difference of sub cls            '         sample.__private_ Cls_attr = _int_direct_changed_to        sample.non_private_ Cls_attr = _int_dirEct_changed_to        assert sample.get__private_cls_attr ()   is 3,             ' Original value of  private attr in cls definition must be unchanged '          assert sample.get_non_private_cls_attr ()  is _int_direct_changed_ to,             ' original value of  Non private attr in cls definition must be changed '          assert samplesubcls.get__private_cls_attr ()  is 33,              ' Super cls private attr can  be changed in sub cls definition,            &Nbsp;on the contrary,nope '         assert samplesubcls._ _private_cls_attr is _int_direct_changed_to,              ' super cls has a  ' "new"  private attr in the  Context,so does sub cls '         assert  samplesubcls.non_private_cls_attr is 33             and samplesubcls.get_non_private_cls_attr ()  is 33,              ' Sub cls overrided non private attr in  super cls and kept it within itself '     def test_ Class_dotted_assignment_effects_on_instance (self):         "cls  dotted assignment to attr out of cls definition generally         changes the relative attr of instance in the same running  context         notice the difference of  sub cls         '          Sample.__private_cls_attr = _INT_DIRECT_CHANGED_TO         Sample.non_private_cls_attr = _INT_DIRECT_CHANGED_TO         assert sample () .__private_cls_attr is _int_direct_changed_to              and sample (). Non_private_cls_attr is  _INT_DIRECT_CHANGED_TO,             ' The  same changes of cls  "new"  attr must be made to its instance '          instance = sample ()         instance. __private_cls_attr = _int_changed_by_instance         instance.non_private_cls_attr = _int_changed_by_instance         assert Sample.__private_cls_attr is _INT_DIRECT_CHANGED_TO              and sample.non_private_cls_attr is _int_direct_ changed_to,             ' The changes of  instance  "New"  attr must not be made to its cls  "         assert samplesubcls (). __private_cls_attr is _ int_direct_changed_to,              ' super cls has a  "new"  private attr in context,so do sub cls and its instance '         assert samplesubcls.get_non_private_cls_attr ()  is  33            and samplesubcls (). Non_ private_cls_attr is 33,             ' Sub  cls overrided non private attr in super cls and kept  It within itself '         instance_sub =  Samplesubcls ()         instance_sub.__private_cls_attr = _int _changed_by_instance        instance_sub.non_private_cls_attr =  _int_changed_by_instance&nbsP;       assert sample.__private_cls_attr is _int_direct_ Changed_to             and sample.non_ private_cls_attr is _int_direct_changed_to,              ' the changes of instance  ' new '  attr must not be  made to its super cls ' if __name__ ==  ' __main__ ':     unittest2.main ()


[Code has the Truth]python class private properties and other points to understand and test the sample code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.