子类化Python类型 [英] subclassing Python types

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

问题描述

我已经读过你可以从基类派生出来,比如str,

list,dict。


我想这会是这样的:


def MyString(str):

def MyList(列表):

def MyDict(dict):

如何访问超类中包含的数据?

I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):
How do you access the data that is contained in the super class?

推荐答案

8月30日晚上8点,zzbba ... @ aol.com写道:
On Aug 30, 8:00 pm, zzbba...@aol.com wrote:

我读过你可以从基类派生出来,比如str,

list,dict。


我想这会是这样的:


def MyString(str):

def MyList(列表):

def MyDict(dict):
I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):



你的意思是


class MyString(str):

....

You mean

class MyString(str):
....


如何访问超类中包含的数据?
How do you access the data that is contained in the super class?



与访问普通字符串,列表或字符的方法相同:使用超类的

方法。


HTH


PS:从这些类型派生一个班级并不是一个好主意。


-

Arnaud

The same way that you access plain strings, lists or dicts: use the
methods of the super class.

HTH

PS: it is not so often a good idea to derive a class from these types.

--
Arnaud


8月30日下午12:13,Arnaud Delobelle< arno ... @ googlemail.comwrote:
On Aug 30, 12:13 pm, Arnaud Delobelle <arno...@googlemail.comwrote:

8月30日晚上8点,zzbba ... @ aol.com写道:
On Aug 30, 8:00 pm, zzbba...@aol.com wrote:

I已经读过你可以从基类派生出来,比如str,

list,dict。
I have read that you can derive from the base classes such as str,
list, dict.


我猜这看起来像:
I guess this would look like:


def MyString( str):

def MyList(list):

def MyDict(dict):
def MyString(str):
def MyList(list):
def MyDict(dict):



你的意思是


class MyString(str):

...


You mean

class MyString(str):
...


如何访问数据包含在超类?
How do you access the data that is contained in the super class?



与访问普通字符串,列表或单词的方式相同:使用超类的

方法。


The same way that you access plain strings, lists or dicts: use the
methods of the super class.



我不知道用什么名字来调用方法:

class MyString(str):

def __init __(strInput):

????? = strInput

I don''t know what name I would use to call a method:
class MyString(str):
def __init__(strInput):
????? = strInput


zz ****** @ aol.com 写道:

我读过你可以从基类派生出来,比如str,

list,dict。


我想这会是这样的:


def MyString(str):

def MyList(list):

def MyDict(dict):

I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):



好​​吧,用'class''替换''def''并且你'''右边。

Well, replace ''def'' with ''class'' and you''re right.


如何访问超类中包含的数据?
How do you access the data that is contained in the super class?



方式:

This way:


>> class MyList(list):
>>class MyList(list):



.... def do_something(self):

.... self.append(3)

....打印自我

....

.... def do_something(self):
.... self.append(3)
.... print self
....


>> l = MyList((1,2))
l
>>l = MyList((1, 2))
l



[1,2]

[1, 2]


>> l.do_something()
>>l.do_something()



[1,2,3]

即:每当你想引用这个值时,请参考self(即,

参考你班级的实例)。


/ W

[1, 2, 3]
That is: Whenever you want to refer to the value refer to self (that is,
refer to the instance of your class).

/W


这篇关于子类化Python类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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