私人数据 [英] Private data

查看:87
本文介绍了私人数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://dustangroups.googlepages.com/...ibutesinpython


这是我今天早上在一个特殊时刻之后扔掉的东西。这是创建私有类属性的方法和

静态函数变量(我不是100%肯定,如果这是正确的

术语,但是你得到我的意思)。我还没有尝试创建

私有实例属性,主要是因为它太难了b / b
很难,而且语法也很糟糕。我不是在考虑使用这个,但我确实对它有几个问题。


1.还有其他人想出过什么东西吗?像这样?我不能想象我是唯一一个曾经想过这个的人。

2.是否有可能入侵这样的事情?也就是说,是否可以从客户端代码中查看和更改这些变量(假设数据管理器已被正确地从视线中删除,如图所示) >
类块的最后一行TestPrivateClassAttributes)?


为了避免完全混淆,你可能想看看

TestPrivateClassAttributes然后再看实际实现

的PrivateDataEngine。如果您仍然感到困惑,请随时询问

这里发生了什么(我愿意接受如何制作它的建议

更清晰)。

解决方案

En Sat,2007年3月17日13:31:01 -0300,Dustan< Du ***** *****@gmail.com>
$ b $bescribió:

http://dustangroups.googlepages.com/...ibutesinpython

这是今天早上我刚刚聚在一起的东西在一个特殊的时刻之后。这是创建私有类属性的方法和

静态函数变量(我不是100%肯定,如果这是正确的

术语,但是你得到我的意思)。我还没有尝试创建

私有实例属性,主要是因为它太难了b / b
很难,而且语法也很糟糕。我实际上并没有考虑使用这个,但我确实有几个问题。



我觉得自己很笨,但我看不出如何使用它,或者是什么。也许是一个

的例子?


-

Gabriel Genellina


2007年3月17日星期六09:31:01 -0700,Dustan写道:

http://dustangroups.googlepages.com/...ibutesinpython


这是我刚刚一起扔的东西今天早上,在一个特殊的时刻之后。这是创建私有类属性的方法和

静态函数变量(我不是100%肯定,如果这是正确的

术语,但是你得到我的意思)。我还没有尝试创建

私有实例属性,主要是因为它太难了b / b
很难,而且语法也很糟糕。我不是在考虑使用这个,但我确实对它有几个问题。


1.还有其他人想出过什么东西吗?像这样?我不能想象我是唯一一个曾经想过这个的人。



我之前从未见过这样的事情,但是我没有去寻找

这样的事情。


2.是否有可能入侵这样的事情?也就是说,是否可以从客户端代码中查看和更改这些变量(假设数据管理器已被正确地从视线中删除,如图所示) >
类块的最后一行TestPrivateClassAttributes)?



是。


首先,代码的一个例子。


>> import PrivateAttributes
obj = PrivateAttributes.TestPrivateClassAttributes()
obj.getNumInstances()



1


>>另一个= PrivateAttributes.TestPrivateClassAttributes()
obj.getNumInstances()



2


>> athird = PrivateAttributes.TestPrivateClassAttributes()
athird.getNumInstances ()



3


getNumInstances方法报告实例数

PrivateAttributes类。这个

计数没有明显的类属性:


>> obj .__ class __.__ dict __。keys()



[''__module__'',' 'getNumInstances'',''__ dict__'',''_ _ _ _ _ _ _ _ _''''''''''''_ _ _ _ _ _''''','$ _ $ _'''
''
这是'怎么样破解它,并报告错误的数字。


>> c = obj .getNumInstances.func_closure
c [1] .cell_contents.numInstances = -300

athird.getNumInstances()



-300


>> afourth = PrivateAttributes.TestPrivateClassAttribut es()
athird.getNumInstances()



-299


所以,是的,它绝对是可以破解的。


现在,我不是一个Python大师,但在大约十五分钟后,我跟着

小道穿过了对象链,并发现如何破解这个。一个真正的大师

可能会在三分钟内完成。


我得到了源代码的帮助。但即使没有来源

代码,我估计我可以在一个小时左右完成它,如果我有动力

足够的话。你需要的所有工具都是Python交互式会话,dir()

函数和dis模块。


-

史蒂文


3月18日上午5:26,Gabriel Genellina < gagsl -... @ yahoo.com.ar>

写道:


En Sat,2007年3月17日13:31: 01 -0300,Dustan< DustanGro ... @ gmail.com>
$ b $bescribió:

http://dustangroups.googlepages.com/...ibutesinpython


这是我今天早上在一个特殊时刻之后刚刚聚集在一起的事情。这是创建私有类属性的方法和

静态函数变量(我不是100%肯定,如果这是正确的

术语,但是你得到我的意思)。我还没有尝试创建

私有实例属性,主要是因为它太难了b / b
很难,而且语法也很糟糕。我实际上并没有考虑使用这个,但我确实有几个问题。



我感到很愚蠢,但我看不出如何使用它,或者是什么。也许是一个

的例子?


-

Gabriel Genellina



有两个例子 - 一个展示静态函数变量

和一个展示私有类属性。两者都执行某种类型的计算。该函数返回它被调用的次数。

这是:


@PrivateDataEngine(numCalls = 0)

def testPrivateStaticFunctionVariables(internalData):

"""返回此函数被调用的次数。""

internalData.numCalls + = 1

返回internalData.numCalls


有一条评论解释装饰器如何在页面上工作我

链接你来。


一个常见的pythonic解决方法是这样的:


def testMutableDefaultArgs(_cache = {''internalData'': 0}):

"""返回此函数被调用的次数。""

_cache [''internalData'' ] + = 1

返回_cache [''internalData'']


默认参数只被评估一次,因为它是可变的

并在f中进行修改在一次通话之后神奇地变成了

{''internalData'':1}和

1348372之后的{''internalData'':1348372}。


另一个解释私有类属性的例子有一个方法

getNumInstances,它返回已创建的实例数。

这是:


class TestPrivateClassAttributes(object):

#pca这里显然代表私有类属性。

pcaManager = PrivateDataEngine(numInstances = 0)


#请注意,内部数据是一个隐含的参数,

首先出现,

#甚至在自我之前参数。

@pcaManager

def __init __(internalData,self):

internalData.numInstances + = 1


@staticmethod

@pcaManager

def getNumInstances(internalData):

返回internalData.numInstances


#别忘了删除pcvManager,或者它将全部是白白的b $ b。

del pcaManager


我想这会是相当的文字得到的时候很难看。

包裹;这就是我首先链接到外部页面的原因。

请注意,如果你想要多个函数,它需要更高级的PrivateDataEngine用法可以访问相同的数据,因为你通常会在一个类中使用



另请注意,双重装饰方法getNumInstances需要

表示staticmethod是第一个装饰者。这相当于

代码getNumInstances = staticmethod(pcaManager(getNumInstances))。当我用pcaManager在staticmethod之前尝试它时,我发现了
错误,

所以我的教育猜测是静态方法以某种方式特别是

被认可因此,他们必须实际上是一个静态方法,而不是由pcaManager打扮过来。


现在我想到了它,为清楚起见,pcaManager可能对于PrivateDataEngine返回的函数来说,这不是一个好的名字; pcaDecorator

会更好看,因为它是一个装饰者。也许我会很快改变你的价格。


pythonic的方式是:


class TestPythonsPrivateClassAttributes(对象):

_numInstances = 0

def __init __(self):

#class属性可以从self访问。

self.numInstances + = 1

@staticmethod

def getNumInstances(internalData):

返回self.numInstances


它应该在语法上更加简洁明了,因此更容易出错。

显然,这些例子很像Hello,World!。在他们的

有用性;一个人很少想跟踪一个

函数的调用次数。


希望有所帮助。


http://dustangroups.googlepages.com/...ibutesinpython

This is something that I just threw together this morning, after a
eureka moment. It''s a way of creating private class attributes and
static function variables (I''m not 100% sure if that''s the correct
terminology, but you get what I mean). I haven''t tried to create
private instance attributes, mainly because it would just be too
difficult, and it would be awful syntax. I''m not considering actually
using this, but I do have a couple questions about it.

1. Has anyone else ever come up with something like this? I can''t
imagine I''m the only person who''s ever thought of this.
2. Is it possible to hack into something like this? ie, would it be
possible to see and change these variables from client code (assuming
the data manager has been properly removed from sight, as shown on the
last line of class block TestPrivateClassAttributes)?

To avoid utter confusion, you may want to look at
TestPrivateClassAttributes before looking at the actual implementation
of PrivateDataEngine. If you''re still confused, feel free to ask
what''s going on here (and I''m open to suggestions on how to make it
clearer).

解决方案

En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <Du**********@gmail.com>
escribió:

http://dustangroups.googlepages.com/...ibutesinpython

This is something that I just threw together this morning, after a
eureka moment. It''s a way of creating private class attributes and
static function variables (I''m not 100% sure if that''s the correct
terminology, but you get what I mean). I haven''t tried to create
private instance attributes, mainly because it would just be too
difficult, and it would be awful syntax. I''m not considering actually
using this, but I do have a couple questions about it.

I feel so dumb, but I can''t see how to use it, or what''s for. Perhaps an
example?

--
Gabriel Genellina


On Sat, 17 Mar 2007 09:31:01 -0700, Dustan wrote:

http://dustangroups.googlepages.com/...ibutesinpython

This is something that I just threw together this morning, after a
eureka moment. It''s a way of creating private class attributes and
static function variables (I''m not 100% sure if that''s the correct
terminology, but you get what I mean). I haven''t tried to create
private instance attributes, mainly because it would just be too
difficult, and it would be awful syntax. I''m not considering actually
using this, but I do have a couple questions about it.

1. Has anyone else ever come up with something like this? I can''t
imagine I''m the only person who''s ever thought of this.

I''ve never seen anything like this before, but then I haven''t gone looking
for anything like this.

2. Is it possible to hack into something like this? ie, would it be
possible to see and change these variables from client code (assuming
the data manager has been properly removed from sight, as shown on the
last line of class block TestPrivateClassAttributes)?

Yes.

First, an example of the code in action.

>>import PrivateAttributes
obj = PrivateAttributes.TestPrivateClassAttributes()
obj.getNumInstances()

1

>>another = PrivateAttributes.TestPrivateClassAttributes()
obj.getNumInstances()

2

>>athird = PrivateAttributes.TestPrivateClassAttributes()
athird.getNumInstances()

3

The getNumInstances method reports the number of instances of the
PrivateAttributes class. There''s no obvious class attribute where this
count is being kept:

>>obj.__class__.__dict__.keys()

[''__module__'', ''getNumInstances'', ''__dict__'', ''__weakref__'', ''__doc__'',
''__init__'']
Here''s how to hack it, and make it report wrong numbers.

>>c = obj.getNumInstances.func_closure
c[1].cell_contents.numInstances = -300

athird.getNumInstances()

-300

>>afourth = PrivateAttributes.TestPrivateClassAttributes()
athird.getNumInstances()

-299

So yes, it is absolutely hackable.

Now, I''m hardly a Python guru, but in about fifteen minutes I followed the
trail through the object chain, and found how to hack this. An real guru
would probably do it in three minutes.

I was helped a bit by having the source code. But even without the source
code, I reckon I could have done it in an hour or so, if I was motivated
enough. All the tools you need are a Python interactive session, the dir()
function and the dis module.

--
Steven


On Mar 18, 5:26 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <DustanGro...@gmail.com>
escribió:

http://dustangroups.googlepages.com/...ibutesinpython

This is something that I just threw together this morning, after a
eureka moment. It''s a way of creating private class attributes and
static function variables (I''m not 100% sure if that''s the correct
terminology, but you get what I mean). I haven''t tried to create
private instance attributes, mainly because it would just be too
difficult, and it would be awful syntax. I''m not considering actually
using this, but I do have a couple questions about it.


I feel so dumb, but I can''t see how to use it, or what''s for. Perhaps an
example?

--
Gabriel Genellina

There are two examples - one demonstrating static function variables
and one demonstrating private class attributes. Both perform some kind
of counting. The function returns how many times it''s been called.
Here it is:

@PrivateDataEngine(numCalls = 0)
def testPrivateStaticFunctionVariables(internalData):
"""returns the number of times this function has been called."""
internalData.numCalls += 1
return internalData.numCalls

There''s a comment explaining how the decorator works on the page I
linked you to.

A common pythonic workaround for this is something like this:

def testMutableDefaultArgs(_cache = {''internalData'':0}):
"""returns the number of times this function has been called."""
_cache[''internalData''] += 1
return _cache[''internalData'']

The default argument only gets evaluated once, and since it''s mutable
and being modified in the function, it magically becomes
{''internalData'':1} after 1 call and {''internalData'':1348372} after
1348372 calls.

The other example, explaining private class attributes, has a method
getNumInstances that returns how many instances have been created.
Here that is:

class TestPrivateClassAttributes(object):
# pca here obviously stands for Private Class Attributes.
pcaManager = PrivateDataEngine(numInstances = 0)

# Notice that the internal data is an implicit parameter that
comes first,
# even before the self parameter.
@pcaManager
def __init__(internalData, self):
internalData.numInstances += 1

@staticmethod
@pcaManager
def getNumInstances(internalData):
return internalData.numInstances

# Don''t forget to delete the pcvManager, or it will have all been
in vain.
del pcaManager

I''m thinking this is going to look rather ugly when the text gets
wrapped; that''s why I linked to an external page in the first place.

Notice it requires more advanced usage of the PrivateDataEngine if you
want multiple functions to have access to the same data, as you
normally would in a class.

Note also that the double-decorated method getNumInstances requires
that staticmethod be the first decorator. This is equivalent to the
code "getNumInstances = staticmethod(pcaManager(getNumInstances))". I
got an error when I tried it with pcaManager preceding staticmethod,
so my educated guess is that staticmethods are somehow specially
recognized by the class, and therefore they have to actually BE
staticmethods, not dressed over by a pcaManager.

Now that I think of it, for clarity, pcaManager may not have been a
good name for the function returned by PrivateDataEngine; pcaDecorator
would have been better, seeing as it IS a decorator. Perhaps I''ll
change that soon.

The pythonic way would be:

class TestPythonsPrivateClassAttributes(object):
_numInstances = 0

def __init__(self):
# class attributes can be accessed from self.
self.numInstances += 1

@staticmethod
def getNumInstances(internalData):
return self.numInstances

It should immediately stick out as being much more concise
syntactically, and therefore much less bug-prone.

Obviously, these examples are a lot like "Hello, World!" in their
usefulness; one would very rarely want to track how many times a
function has been called.

Hope that helps.


这篇关于私人数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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