[英]is this code truly private? (python)
我試圖讓python允許私有變量,所以我把這個裝飾器放在一個類的乞討中,這樣每個函數都會獲得一個額外的私有參數,他們可以修改它們是他們想要的。 據我所知,從課外獲取變量是不可能的,但我不是專業人士。
任何人都可以找到一種方法來入侵私有對象並從中獲取值? 有沒有比這更好的方法呢?
python 2.7
#this is a decorator that decorates another decorator. it makes the decorator
#not loose things like names and documentation when it creates a new function
def niceDecorator(decorator):
def new_decorator(f):
g = decorator(f)
g.__name__ = f.__name__
g.__doc__ = f.__doc__
g.__dict__.update(f.__dict__)
return g
new_decorator.__name__ = decorator.__name__
new_decorator.__doc__ = decorator.__doc__
new_decorator.__dict__.update(decorator.__dict__)
return new_decorator
@niceDecorator
#this is my private decorator
def usePrivate(cls):
prv=type('blank', (object,), {})
#creates a blank object in the local scope
#this object will be passed into every function in
#the class along with self which has been renamed
#as pbl (public).
@niceDecorator
#this is the decorator that gets applied to every function
#in the class. in makes it also accept the private argument
def decorate(func):
def run(pub, *args, **kwargs):
return func(pub,prv, *args, **kwargs)
return run
#this loops through every function in the class and applies the decorator
for func in cls.__dict__.values():
if callable(func):
setattr(cls, func.__name__, decorate(getattr(cls, func.__name__)))
return cls
#this is the class we are testing the private decorator with.
#this is what the user would program
@usePrivate
class test():
#sets the value of the private variable
def setValue(pbl,prv,arg):
#pbl (public) is another name for self
#prv (private) acts just like self except its private
prv.test=arg
#gets the value of the private variable
def getValue(pbl,prv):
return prv.test
a=test()
a.setValue(3)
print a.getValue()
簡而言之:不要這樣做。
沒有必要在Python中使事物真正私有化 。 使用您的軟件的人可以看到某些內容是否被標記為私有(變量名稱以_
開頭),因此他們知道。 如果他們仍想訪問它,為什么要阻止它們呢?
我確信你的代碼也有辦法 - Python擁有大量內省代碼,修改類很容易。 如果有人真的想要了解它,幾乎不可能鎖定任何東西。
值得注意的是,在Python中,setter / getter毫無意義。 目的是允許您在設置/獲取屬性時添加代碼,python允許您使用property()
內置 。
這是一個有趣的想法,但是您用於裝飾器的包裝器函數將在其func_closure
屬性中引用“private”對象。 因此,您的“私有”變量可以作為a.getValue.func_closure[0].cell_contents.test
。 (您可以使用任何包裝函數來訪問“私有”對象,而不僅僅是getValue
。)
通常,這種技術只會惹惱使用您代碼的其他程序員。
總有辦法在Python中處理事情,特別是如果你有原始資源來閱讀。 使用kindall的示例,將這些行添加到文件的末尾:
print a.getValue.im_func.func_closure[0].cell_contents.test
a.getValue.im_func.func_closure[0].cell_contents.test = 17
print a.getValue()
真的,不要這樣做。 Python人們說,“不要為私有變量而煩惱”。
正如其他人所說,仍有一種方法可以獲得私有變量。 但是,您仍然可以使用c ++獲取私有變量。 考慮一下這個C ++示例:
class PrivateEye
{
private:
int a;
double b;
char c;
public:
// ... public functions ...
};
PrivateEye detective;
double privateB = *((double *) ((void *) &detective + sizeof(detective.a)));
正如您所看到的,訪問私有變量需要很多工作,因此這樣做的人需要足夠的知識來了解風險。 因此,如果您有程序員使用您的_attribute
私有屬性,那么您發布的解決方案將有助於他們在弄亂私有屬性之前考慮它。 使用__attribute
(雙下划線)將導致一些名稱重整,這也會導致人們在決定訪問“私有”屬性之前進行更多思考。
編輯:根據訪問私有成員的第二個答案,C ++標准不保證類中成員變量的順序,因此您可能需要進行一些實驗以獲取對C ++示例中所需的私有變量的訪問權限以上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.