Python “protected” attributes

后端 未结 7 1348
迷失自我
迷失自我 2020-12-13 03:39

How do I access a private attribute of a parent class from a subclass (without making it public)?

7条回答
  •  孤街浪徒
    2020-12-13 04:34

    I think this code is a little clearer than Steve's. Steve's answer was most helpful for what I am trying to do, so thank you! I tested this with python 2.7 and python 3.6.

    #! /usr/bin/python
    #
    # From https://stackoverflow.com/questions/797771/python-protected-attributes
    from __future__ import print_function
    import sys
    
    class Stock(object):
    
        def __init__(self, stockName):
    
            # '_' is just a convention and does nothing
            self.__stockName  = stockName   # private now
    
    
        @property # when you do Stock.name, it will call this function
        def name(self):
            print("In the getter, __stockName is %s" % self.__stockName, file=sys.stderr)
            return self.__stockName
    
        @name.setter # when you do Stock.name = x, it will call this function
        def name(self, name):
            print("In the setter, name is %s will become %s" % ( self.__stockName, name), file=sys.stderr)
            self.__stockName = name
    
    if __name__ == "__main__":
        myStock = Stock("stock111")
    
        try:
            myStock.__stockName  # It is private. You can't access it.
        except AttributeError as a:
            print("As expect, raised AttributeError", str(a), file=sys.stderr )
        else:
            print("myStock.__stockName did did *not* raise an AttributeError exception")
    
    
        #Now you can myStock.name
        myStock.name = "Murphy"
        N = float(input("input to your stock: " + str(myStock.name)+" ? "))
        print("The value of %s is %s" % (myStock.name, N) )
    

提交回复
热议问题