我来自Java世界,正在阅读Bruce Eckels的《Python 3 Patterns, Recipes and idiom》。
在阅读有关类的内容时,会继续说在Python中不需要声明实例变量。你只需要在构造函数中使用它们,它们就在那里了。
例如:
class Simple:
def __init__(self, s):
print("inside the simple constructor")
self.s = s
def show(self):
print(self.s)
def showMsg(self, msg):
print(msg + ':', self.show())
如果这是真的,那么任何Simple类的对象都可以改变类外变量s的值。
例如:
if __name__ == "__main__":
x = Simple("constructor argument")
x.s = "test15" # this changes the value
x.show()
x.showMsg("A message")
在Java中,我们已经学习了公共/私有/受保护变量。这些关键字是有意义的,因为有时您希望类中的变量在类之外没有人可以访问。
为什么Python中不需要这个?
私有变量在Python中或多或少是一种hack:解释器会故意重命名变量。
class A:
def __init__(self):
self.__var = 123
def printVar(self):
print self.__var
现在,如果你试图在类定义之外访问__var,它会失败:
>>> x = A()
>>> x.__var # this will return error: "A has no attribute __var"
>>> x.printVar() # this gives back 123
但你可以很容易地摆脱这种情况:
>>> x.__dict__ # this will show everything that is contained in object x
# which in this case is something like {'_A__var' : 123}
>>> x._A__var = 456 # you now know the masked name of private variables
>>> x.printVar() # this gives back 456
你可能知道OOP中的方法是这样调用的:x. printvar () => A.printVar(x)。如果A.printVar()可以访问x中的某个字段,那么这个字段也可以在A.printVar()之外访问…毕竟,函数是为可重用性而创建的,其中的语句并没有任何特殊的功能。
“在java中,我们学习了公共/私有/受保护变量”
“为什么在python中不需要这个?”
出于同样的原因,它在Java中不是必需的。
您可以自由使用或不使用private和protected。
作为Python和Java程序员,我发现私有和受保护是非常非常重要的设计概念。但实际上,在数万行Java和Python代码中,我从未真正使用过private或protected。
为什么不呢?
我的问题是"被谁保护?"
我团队中的其他程序员?他们有消息来源。当他们可以改变的时候,保护意味着什么?
其他团队的其他程序员?他们在同一家公司工作。只要打个电话,他们就能找到线人。
客户吗?这是一种雇佣式编程(通常)。客户机(通常)拥有代码。
那么,我到底在保护谁呢?
我是Python的新手但我有c#和JavaScript的背景。就特性而言,Python感觉像是两者的混合体。JavaScript在这方面也有问题,解决方法是创建一个闭包。这可以通过返回不同的对象来防止访问您不想公开的数据。
def print_msg(msg):
# This is the outer enclosing function
def printer():
# This is the nested function
print(msg)
return printer # returns the nested function
# Now let's try calling this function.
# Output: Hello
another = print_msg("Hello")
another()
https://www.programiz.com/python-programming/closure
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#emulating_private_methods_with_closures