首页 > 解决方案 > 课堂上无法理解的东西

问题描述

课堂上有些难以理解的东西。

   From aiogram.types import InputFile
   
   a = InputFile(r'C:\Users\Acer\Desktop\f\1.mp3')
   b = InputFile(r'C:\Users\Acer\Desktop\f\2.mp3')
   c = InputFile(r'C:\Users\Acer\Desktop\f\3.mp3')
   d = InputFile(r'C:\Users\Acer\Desktop\f\4.mp3')
   e = InputFile(r'C:\Users\Acer\Desktop\f\5.mp3')
   
   class Make:
       def __init__(self, art, zanr, name, put):
       self.art = art
       self.zanr = zanr
       self.name = name
       self.put = put
   
   a1 = Make('Blaze', 'Hip-hop', 'Music_1234', a)
   b1 = Make('PORCHY', 'Rap', 'Trak_28', b)
   c1 = Make('Blaze', 'Pop', 'North-abc', c)
   d1 = Make('PORCHY', 'Hip-hop', '26-83', d)
   e1 = Make('Blaze', 'Pop', 'Abnov', e)
   
   spis = [a1, b1, c1, d1, e1]
   
   for x in range(0, 4):
       if spis[x].art == 'PORCHY':
           print(cl.spis[x].put)

控制台:

<InputFile 'attach://GqEaqV-Wj3zekeFP9GIygA' with file='<_io.BufferedReader name='C:\\Users\\Acer\\Desktop\\f\\2.mp3'>'>
<InputFile 'attach://JeYgSuIUgVjS7nDwPONivQ' with file='<_io.BufferedReader name='C:\\Users\\Acer\\Desktop\\f\\4.mp3'>'>

为什么不呢:

C:\Users\Acer\Desktop\f\2.mp3
C:\Users\Acer\Desktop\f\4.mp3

标签: pythonclassoopimportpython-class

解决方案


通过查看InputFile 源代码,您可以看到该__str__方法的定义如下:

def __str__(self):
        return f"<InputFile 'attach://{self.attachment_key}' with file='{self.file}'>"

并且__repr__方法等于__str__

如果您只想打印文件的路径,我相信您应该使用

print(cl.spis[x].put.get_filename())

推荐阅读