这段代码真的是私密的吗?(Python) [英] is this code truly private? (python)

查看:47
本文介绍了这段代码真的是私密的吗?(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 python 允许私有变量,所以我做了这个装饰器,你把它放在一个类的乞求中,这样每个函数都会得到一个额外的私有参数,他们可以修改为他们想要的.据我所知,不可能从课外获取变量,但我不是专业人士.

I am trying to make python allow private variable, so I made this decorator that you put at the begging of a class so that every function will get an additional private parameter that they can modify to be what they want. as far as I can tell, it is impossible to get the variables from outside the class, but I'm not a pro.

任何人都可以找到一种方法来入侵私有对象并从中获取值吗?还有比这更好的方法吗?

can anyone find a way to hack into the private object and get the values from it? is there a better way to do it than this?

蟒蛇 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()

推荐答案

这是一个有趣的想法,但是您用于装饰器的包装函数将在其 func_closure<中引用私有"对象/code> 属性.所以你的私有"变量可以作为 a.getValue.func_closure[0].cell_contents.test 访问.(您可以使用任何包装函数来访问您的私有"对象,而不仅仅是 getValue.)

That's an interesting idea, but the wrapper functions you're using for the decorator will have a reference to the "private" object in their func_closure attribute. So your "private" variable is accessible as a.getValue.func_closure[0].cell_contents.test. (You can use any wrapped function to get to your "private" object, not just getValue.)

通常这种技术只会惹恼其他正在使用您的代码的程序员.

Generally this sort of technique will only serve to annoy other programmers who are using your code.

这篇关于这段代码真的是私密的吗?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆