鱼C论坛

 找回密码
 立即注册
查看: 1871|回复: 7

python类问题求助

[复制链接]
发表于 2017-11-23 16:32:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
In this task, we develop a class with which elements of a list can be used as often as desired, in cycles. The class Ez (short for "perpetual cycle") should have the following methods:

A constructor to pass any iterable (that is, list, tuple, ...). The default value of this iteration should be None.

A method set_data, which is also passed an Iterable. After calling set_data, this iterable should be used, starting with its first element.

A method next that provides a reference to the next element of the data. If no further data are available, the first element should be started again. When set_data is called, next should always start over again.

First write the constructor and set_data. Consider which attributes an object of this class must remember.
Then write the method next. If the iteration of the Ez object does not contain any elements, or if there is no Iterable, the method should return None.



#这些代码不能更改,用于检验
e = Ez([1,2,3])
assert e.next() == 1
assert e.next() == 2
assert e.next() == 3
assert e.next() == 1

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-11-23 16:48:38 From FishC Mobile | 显示全部楼层
你的代码呢?只给题目?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-23 16:50:49 | 显示全部楼层
BngThea 发表于 2017-11-23 16:48
你的代码呢?只给题目?

没有思路 不知道这个该怎么下手
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-23 16:53:52 From FishC Mobile | 显示全部楼层
wanggz100 发表于 2017-11-23 16:50
没有思路 不知道这个该怎么下手

都告诉你类里面有哪些函数了,每个函数有什么功能了,至少能写出大致代码,完全没有思路那只能重新学习一下课程
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-23 17:00:29 | 显示全部楼层
BngThea 发表于 2017-11-23 16:53
都告诉你类里面有哪些函数了,每个函数有什么功能了,至少能写出大致代码,完全没有思路那只能重新学习一 ...

class Ez:
    def __init__(self,e):
        self.e = None

不知道这个 构造对不对
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-23 17:25:34 | 显示全部楼层
看介绍 完全是在写一个  generator
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-23 17:38:40 | 显示全部楼层
yjsx86 发表于 2017-11-23 17:25
看介绍 完全是在写一个  generator

怎么写 有没有提示。 我刚学一个月 没什么思路
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-23 18:08:47 | 显示全部楼层
  1. class EZ:
  2.     def __init__(self, it):
  3.         self.e = it
  4.    
  5.     def set_data(self):
  6.         self.temp = (i for i in self.e)
  7.    
  8.     def next(self):
  9.         try:
  10.             return next(self.temp)
  11.         except:
  12.             self.set_data()
  13.             return next(self.temp)


  14. e = EZ([1, 2, 3])
  15. assert e.next() == 1
  16. assert e.next() == 2
  17. assert e.next() == 3
  18. assert e.next() == 1
复制代码


根据我的理解是这样的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 02:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表