调用私有基础方法 [英] Calling private base methods

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

问题描述

大家好,


既然我真的深入Python,我会遇到很多事情,我们新手发现很难做到这一点。我以为我理解了

super()是如何运作的,但是对于''私人''成员来说,似乎并不是
工作。例如;


>> class A(object):



.... def __baseMethod(self):

.... print''Test''


从A中获取并执行;


> > class D(A):



.... def someMethod(self):

....超级(A,自我).__ baseMethod()

....打印''test3''


无效;


>> p = D()
p.someMethod()



Traceback(最近一次调用最后一次):

文件"<互动输入>& quot;,第1行,在< module>

文件"<互动输入>",第3行,在someMethod中

AttributeError:''super''对象没有属性''_D__baseMethod''


是否可以调用私有基本方法?我来自C ++

背景,我喜欢这种结构,因为我的基类有帮助器

方法,所以我不必重复代码。


当我这样做时;


>> E级(对象):



.... def someMethod(self):

.... print ''你好''

....


> ;>类F(E):



.... def otherMethod(self):

.... super(F,self).someMethod()

....打印''那里'

....
< blockquote class =post_quotes>


>> p = F()
p.otherMethod()



你好



>>>



这似乎有效。


提前致谢,

- Jorgen

解决方案

" Jorgen Bodde" < jo ************* @ gmail.comha scritto nel messaggio

新闻:ma **************** *********************** @ pyt hon.org ...


大家好,


既然我真的深入Python,我会遇到很多东西

我们的新手很难找到合适的东西。我以为我理解了

super()是如何运作的,但是对于''私人''成员来说,似乎并不是
工作。例如;


> class A(object):



... def __baseMethod(个体经营):

...打印''测试''


从A派生,并且做;


> class D(A):



... def someMethod(self):

... super(A,self).__ baseMethod()

... print''test3''


无效;



如果您键入


> dir(A)



你会得到一个名为
的方法
_A__baseMethod


来自文档:

私有名称修改:当一个文本出现在类中的标识符

定义以两个或多个下划线字符开头并且不以
$ b结尾$ b两个或多个下划线,它被认为是该类的私有名称。

在为

生成代码之前,私有名称将转换为更长的形式。转换在名称前面插入类名,并删除

前导下划线,并在类名前面插入一个下划线。

。例如,名为Ham的类名称__spam将被转换为_Ham__spam。这种转换是
,与使用标识符的语法上下文无关。如果

转换后的名称非常长(超过255个字符),则可能会发生实现定义截断的
。如果类名包含

只有下划线,则不进行转换。


Enrico


4月12日凌晨2点47分,约根博德(Jorgen Bodde) < jorgen.maill ... @ gmail.comwrote:


是否可以调用私有基本方法?我来自C ++

背景,我喜欢这种结构,因为我的基类有帮助器

方法,所以我不必重复代码。



我想看到一些C ++代码可以做到这一点!


" 7stud" < bb ********** @ yahoo.comwrote:


4月12日凌晨2:47,Jorgen Bodde < jorgen.maill ... @ gmail.comwrote:


>是否可以调用私有基本方法?我来自C ++
背景,我喜欢这种结构,因为我的基类有帮助器
方法,所以我不必重复代码。



我想看到一些C ++代码可以做到这一点!



简单:


#define私人公众

#include< someheader>

#undef private


然后调用私有方法你想要的。


Hi All,

Now that I am really diving into Python, I encounter a lot of things
that us newbies find difficult to get right. I thought I understood
how super() worked, but with ''private'' members it does not seem to
work. For example;

>>class A(object):

.... def __baseMethod(self):
.... print ''Test''

Deriving from A, and doing;

>>class D(A):

.... def someMethod(self):
.... super(A, self).__baseMethod()
.... print ''test3''

Will not work;

>>p = D()
p.someMethod()

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 3, in someMethod
AttributeError: ''super'' object has no attribute ''_D__baseMethod''

Is it possible to call a private base method? I come from a C++
background, and I liked this construction as my base class has helper
methods so that I do not have to duplicate code.

When I do;

>>class E(object):

.... def someMethod(self):
.... print ''Hello''
....

>>class F(E):

.... def otherMethod(self):
.... super(F, self).someMethod()
.... print ''There''
....

>>p = F()
p.otherMethod()

Hello
There

>>>

This seems to work.

Thanks in advance,
- Jorgen

解决方案

"Jorgen Bodde" <jo*************@gmail.comha scritto nel messaggio
news:ma***************************************@pyt hon.org...

Hi All,

Now that I am really diving into Python, I encounter a lot of things
that us newbies find difficult to get right. I thought I understood
how super() worked, but with ''private'' members it does not seem to
work. For example;

>class A(object):

... def __baseMethod(self):
... print ''Test''

Deriving from A, and doing;

>class D(A):

... def someMethod(self):
... super(A, self).__baseMethod()
... print ''test3''

Will not work;

if you type

>dir(A)

you''ll get a method named
_A__baseMethod

From the documentation:
Private name mangling: When an identifier that textually occurs in a class
definition begins with two or more underscore characters and does not end in
two or more underscores, it is considered a private name of that class.
Private names are transformed to a longer form before code is generated for
them. The transformation inserts the class name in front of the name, with
leading underscores removed, and a single underscore inserted in front of
the class name. For example, the identifier __spam occurring in a class
named Ham will be transformed to _Ham__spam. This transformation is
independent of the syntactical context in which the identifier is used. If
the transformed name is extremely long (longer than 255 characters),
implementation defined truncation may happen. If the class name consists
only of underscores, no transformation is done.

Enrico


On Apr 12, 2:47 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:

Is it possible to call a private base method? I come from a C++
background, and I liked this construction as my base class has helper
methods so that I do not have to duplicate code.

I''d like to see some C++ code that does that!


"7stud" <bb**********@yahoo.comwrote:

On Apr 12, 2:47 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:

>Is it possible to call a private base method? I come from a C++
background, and I liked this construction as my base class has helper
methods so that I do not have to duplicate code.


I''d like to see some C++ code that does that!

Easy:

#define private public
#include <someheader>
#undef private

then call the private methods as much as you want.


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

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