我们可以使类的属性对公众可见,但只能由某些特定类修改吗?
例如,
// this is the property holder
public class Child
{
public bool IsBeaten { get; set;}
}
// this is the modifier which can set the property of Child instance
public class Father
{
public void BeatChild(Child c)
{
c.IsBeaten = true; // should be no exception
}
}
// this is the observer which can get the property but cannot set.
public class Cat
{
// I want this method always return false.
public bool TryBeatChild(Child c) …Run Code Online (Sandbox Code Playgroud) 考虑以下模式:
'''some module body'''
def __foo():
'''module method foo'''
pass
class Dummy(object):
@staticmethod
def bar():
__foo()
__foo() # No Error.
Dummy.bar() #NameError: Global name "_Dummy__foo" is not defined.
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
-
如果用"__"命名它是不好的,那么Python中使模块方法仅用于内部模块函数/方法的最佳做法是什么?
假设我们有两个列表:
a = [1, 2, 3]
b = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
以下所有表达式都将返回True:
a == b # True
a == list(b) # True
a == list(tuple(b)) # True
a == copy.deepcopy(b) # still True
Run Code Online (Sandbox Code Playgroud)
这里a并b具有相同的元素2个不同的列表对象.我们如何区分彼此?