gpt4 book ai didi

python - Python 中的 "Sub-classes"和 self

转载 作者:太空宇宙 更新时间:2023-11-03 18:44:27 25 4
gpt4 key购买 nike

注意:我发现在询问如何做之前,我需要更清楚地弄清楚我希望每个属性/描述符/类/方法做什么!我认为我的问题目前无法得到解答。感谢大家帮助我。

感谢 icktoofay 和 BrenBarn,我开始了解描述符和属性,但现在我有一个稍微困难的问题要问:

我现在明白它们是如何工作的:

class Blub(object):
def __get__(self, instance, owner):
print('Blub gets ' + instance._blub)
return instance._blub

def __set__(self, instance, value):
print('Blub becomes ' + value)
instance._blub = value

class Quish(object):
blub = Blub()

def __init__(self, value):
self.blub = value

a = Quish('one') 是如何工作的(产生“Blub 变成 one”),但看一下这段代码:

import os
import glob

class Index(object):
def __init__(self, dir=os.getcwd()):
self.name = dir #index name is directory of indexes
# index is the list of indexes
self.index = glob.glob(os.path.join(self.name, 'BatchStarted*'))
# which is the pointer to the index (index[which] == BatchStarted_12312013_115959.txt)
self.which = 0
# self.file = self.File(self.index[self.which])
def get(self):
return self.index[self.which]
def next(self):
self.which += 1
if self.which < len(self.index):
return self.get()
else:
# loop back to the first
self.which = 0
return None
def back(self):
if self.which > 0:
self.which -= 1
return self.get()

class File(object):
def __init__(self, file):
# if the file exists, we'll use it.
if os.path.isfile(file):
self.name = file
# otherwise, our name is none and we return.
else:
self.name = None
return None
# 'file' attribute is the actual file object
self.file = open(self.name, 'r')
self.line = Lines(self.file)

class Lines(object):
# pass through the actual file object (not filename)
def __init__(self, file):
self.file = file
# line is the list if this file's lines
self.line = self.file.readlines()
self.which = 0
self.extension = Extension(self.line[self.which])
def __get__(self):
return self.line[self.which]
def __set__(self, value):
self.which = value
def next(self):
self.which += 1
return self.__get__()
def back(self):
self.which -= 1
return self.__get__()

class Extension(object):
def __init__(self, lineStr):
# check to make sure a string is passed
if lineStr:
self.lineStr = lineStr
self.line = self.lineStr.split('|')
self.pathStr = self.line[0]
self.path = self.pathStr.split('\\')
self.fileStr = self.path[-1]
self.file = self.fileStr.split('.')
else:
self.lineStr = None
def __get__(self):
self.line = self.lineStr.split('|')
self.pathStr = self.line[0]
self.path = self.pathStr.split('\\')
self.fileStr = self.path[-1]
self.file = self.fileStr.split('.')
return self.file[-1]
def __set__(self, ext):
self.file[-1] = ext
self.fileStr = '.'.join(self.file)
self.path[-1] = fileStr
self.pathStr = '\\'.join(self.path)
self.line[0] = self.pathStr
self.lineStr = '|'.join(self.line)

首先,这里可能有一些拼写错误,因为我一直在努力,但半途而废。这不是我的重点。我的观点是,在 icktoofay 的示例中,没有任何内容传递给 Blub()。有什么方法可以完成我在这里所做的事情,即设置一些“ self ”属性,并在进行一些处理后,将其传递给下一个类?这会更适合特性吗?

我想要这样:

>>> i = Index()         # i contains list of index files
>>> f = File(i.get()) # f is now one of those files
>>> f.line
'\\\\server\\share\\folder\\file0.txt|Name|Sean|Date|10-20-2000|Type|1'
>>> f.line.extension
'txt'
>>> f.line.extension = 'rtf'
>>> f.line
'\\\\server\\share\\folder\\file0.rtf|Name|Sean|Date|10-20-2000|Type|1'

最佳答案

您可以这样做,但问题较少涉及属性/描述符,而更多涉及创建提供您想要的行为的类。

所以,当你执行f.line时,那就是某个对象。当您执行f.line.extension时,即执行(f.line).extension——也就是说,它首先评估f.line code> 然后获取 f.lineextension 属性。

这里重要的是,f.line 无法知道您稍后是否会尝试访问其扩展。因此,您不能让 f.line 为“plain”f.line 做一件事,而为 f.line.extension 做另一件事。两者中的 f.line 部分必须相同,并且 extension 部分无法更改这一点。

您似乎想要做的解决方案是使 f.line 返回某种对象,该对象在某种程度上看起来或工作起来像字符串,但也允许设置属性和更新自身因此。具体如何执行此操作取决于您需要多少 f.lines 才能表现得像字符串,以及您需要多少它来执行其他操作。基本上,您需要 f.line 成为一个“看门人”对象,通过像字符串一样处理某些操作(例如,您显然希望它显示为字符串),并以自定义方式处理其他对象(例如,您显然希望能够在其上设置 extension 属性并更新其内容)。

这是一个简单的示例:

class Line(object):
def __init__(self, txt):
self.base, self.extension = txt.split('.')

def __str__(self):
return self.base + "." + self.extension

现在你可以做:

>>> line = Line('file.txt')
>>> print line
file.txt
>>> line.extension
'txt'
>>> line.extension = 'foo'
>>> print line
file.foo

但是,请注意,我打印了,而不仅仅是。通过编写 __str__ 方法,我定义了 print line 时发生的行为。但是,如果您“原始”评估它而不打印它,您会发现它并不是真正的字符串:

>>> line
<__main__.Line object at 0x000000000233D278>

您也可以覆盖此行为(通过定义__repr__),但是您想这样做吗?这取决于您想要如何使用line。关键是您需要决定您希望您的line在什么情况下执行什么操作,然后创建一个类来执行该操作。

关于python - Python 中的 "Sub-classes"和 self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com