当我应用两个下划线时,出现错误AttributeError: 'Organization' object has no attribute '__employees'
Here is the code。
class Organization(object):
__employees=[]
google=Organization()
google.__employees.append('Erik')
Run Code Online (Sandbox Code Playgroud)
Python 没有实现私有变量的概念。如果是这样,我会得到什么错误。如果我删除一个下划线代码执行没有错误。
当我在私有变量链接上阅读python文档时,我有一个问题.
因此,文档说明使用下划线命名私有变量是一种约定,但python不会使该字段为私有.
>>> class a():
def __init__(self):
self.public = 11
self._priv = 12
>>> b = a()
>>> print b._priv
>>> 12
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以在python中使变量"真正"私有.
在模块中,我应该使用一个,两个或没有下划线表示一个辅助功能的用户应该不叫?
我在python中创建了基于抽象的类,它基于所有子类的类,并实现了一些在每个子类中每次都要写入多余的函数.
class Element:
###SITE###
__sitedefs = [None]
def getSitedefs(self):
return self.__sitedefs
class SRL16(Element):
###SITE###
__sitedefs = ['SLICEM']
Run Code Online (Sandbox Code Playgroud)
结果是合乎逻辑的,因为我从基础类中获取值,我声明了值,但否则我在子项1中覆盖它.我的问题是如何得到
srl = SRL16()
srl.getSitedefs()
Run Code Online (Sandbox Code Playgroud)
SLICEM不是
可能我会错过一些非常基础的东西,但请帮忙.
最好的祝福
我记得有一种语法可以使一个类的函数只能由该类调用,而不是任何继承该类函数的子类,但我不记得究竟它是什么,谷歌是什么对我没有帮助.任何人都可以提醒我如何做到这一点?
我花了一天的时间试图找到这个问题的答案,但一无所获。
假设我有几个类,每个类都包含与其他类相同的方法。
class A:
def __init__(self, attr1, attr2, color):
self.__attr1 = attr1
self.__attr2 = attr2
self.__color = color
def set_color(self, color):
self.__color = color
class B:
def __init__(name, attr3, attr4, color):
self.__attr3 = attr3
self.__attr4 = attr4
self.__color = color
def set_color(self, color):
self.__color = color
Run Code Online (Sandbox Code Playgroud)
在此示例中,相同的方法被简化为只有一个相同的方法 set_color,以说明我正在尝试执行的操作。
有没有办法消除重复代码,也许使用如下抽象类?
class Parent:
def set_color(self, color):
self.__color = color
class A(Parent):
def __init__(self, attr1, attr2, color):
self.__attr1 = attr1
self.__attr2 = attr2
self.__color = color
class B(Parent):
def __init__(name, attr3, attr4, color): …Run Code Online (Sandbox Code Playgroud) 假设我有一个类定义,它接受一些参数并用它创建额外的数据,所有这些都在__init__方法中:
class Foo():
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self.bar = generate_bar(x, y, z)
def generate_bar(self, x, y, z):
return x+y+z
Run Code Online (Sandbox Code Playgroud)
我只想在generate_bar()创建类的实例时运行该方法一次,因此该方法在实例上可调用是没有意义的.是否有更简洁的方法来确保方法仅可用__init__,或者我只需要假设任何查看我的代码的人都会理解,从来没有一种情况可以在一个实例上调用generate_bar().班级?
演示了如何隐藏类的属性和方法来访问类外的隐藏变量
class hiding():
# class attribute, "__" befor an attribute will make it to hide
__hideAttr = 10
def func(self):
self.__hideAttr += 1
print(self.__hideAttr)
a = hiding()
a.func()
a.func()
#AttributeError: 'hiding' object has no attribute '__hideAttr'
print (a.__hideAttr)
Run Code Online (Sandbox Code Playgroud) 我在写一个银行应用程序Python,并从这里读了一些源代码的银行应用程序.该balance课程定义如下:
class Balance(object):
""" the balance class includes the balance operations """
def __init__(self):
""" instantiate the class """
self.total = 0
def add(self, value):
""" add value to the total
Args:
value (int): numeric value
"""
value = int(value)
self.total += value
def subtract(self, value):
""" subtract value from the total
Args:
value (int): numeric value
"""
value = int(value)
self.total -= value
Run Code Online (Sandbox Code Playgroud)
我的问题
由于不应该在类之外访问余额细节,我们应该定义属性self.total,self.__total因为我们应该使它变得private相当public可变?我的思路是否正确?
我有一个代表请求的类.我有一组非常具体的请求,例如"I-95","P-22"等,它们执行不同的功能并由控制器类调用.这样做的最佳方法是什么,以便人们可以轻松地在路上添加更多请求?
我现在有这样的事情:
class Requests:
def __init__(self):
self.types = [
'I-95',
'P-22',
...
]
def generate_request(self,type, Data):
# Here I would call the appropriate case for the type, e.g. P-22
Run Code Online (Sandbox Code Playgroud)
请求案例将在其自己的单独文件中,如下所示:
class P-22:
# Members
def __init__(self, Data):
# Set data members
def request(self):
# Submit request
Run Code Online (Sandbox Code Playgroud)
我可以在控制器中创建一个请求
f = Requests()
f.generate_request('RC75')
Run Code Online (Sandbox Code Playgroud)
我正在努力寻找尽可能干净且易于扩展的东西.谢谢!