《每日论文》Problem Solving with Algorithms and Data Structures using Python.(第二章)

Problem Solving with Algorithms and Data Structures using Python.使用Python解决算法和数据结构的问题。

在这里插入图片描述
By Brad Miller and David Ranum, Luther College

2. A Proper Class

2. 一个合适的类

2.1. Writing a Proper Python Class

2.1. 编写合适的Python类

When you write a class there are a lot of things to consider.
当您编写一个类时,有很多事情要考虑。
Especially if you are going to release your class for others to use.
特别是当您打算发布您的类供他人使用时。
In this section we will build a simple class to represent a die that you can roll, and a cup to contain a bunch of dice.
在本节中,我们将构建一个简单的类来表示一个可以掷的骰子,以及一个包含一堆骰子的杯子。
We will incrementatlly improve our implementations to take into consderation the following aspects of desiging a class that works well in the Python ecosystem.
我们将逐步改进我们的实现,在设计一个在Python生态系统中工作良好的类时考虑以下几个方面。

  • Each class should have a docstring to provide some level of documentation on how to use the class.每个类都应该有一个文档字符串,以提供关于如何使用类的某种级别的文档。
  • Each class should have a str magic method to give it a meaninigful string representation.每个类都应该有一个__str__魔法方法来给它一个有意义的字符串表示。
  • Each class should have a proper repr magic method for representation in the interactive shell, the debugger, and other cases where string conversion does not happen.每个类都应该有一个适当的__repr__魔法方法,用于在交互式shell、调试器和其他字符串转换不发生的情况下表示。
  • Each class should be comparable so it can be sorted and meaningfully compared with other instances. At a minimum this means implementing eq and lt.每个类都应该具有可比性,这样就可以对它进行排序,并与其他实例进行有意义的比较。这至少意味着要实现__eq__和__lt__。
  • You should think about access control each instance variable. Which attributes do you want to make public, which attributes do you want to make read only, and which attributes do you want to control or do value checking on before you allow them to be changed.您应该考虑对每个实例变量的访问控制。哪些属性要公开,哪些属性要只读,哪些属性在允许更改之前要控制或进行值检查。

If the class is a container for other classes then there are some further considerations:
如果该类是其他类的容器,则需要进一步考虑:

  • You should be able to find out how many things the container holds using len您应该能够通过使用len来找出容器容纳了多少东西
  • You should be able to iterate over the items in the container.您应该能够遍历容器中的项。
  • You may want to allow users to access the items in the container using the square bracket index notation.您可能希望允许用户使用方括号索引符号访问容器中的项。
2.1.1. A Basic implementation of the MSDie class
2.1.1. MSDie类的基本实现

Lets start with a really simple implementation of the MSDie class, and we’ll improve it one step at a time.
让我们从MSDie类的一个真正简单的实现开始,然后一步一步地改进它。
We want to make our die a bit flexible so the constructor will allow us to specify the number of sides.
我们想让我们的模具稍微灵活一点,这样构造函数将允许我们指定边的数量。

import random

class MSDie:
    """
    Multi-sided die

    Instance Variables:
        current_value
        num_sides

    """

    def __init__(self, num_sides):
        self.num_sides = num_sides
        self.current_value = self.roll()

    def roll(self):
        self.current_value = random.randrange(1,self.num_sides+1)
        return self.current_value

my_die = MSDie(6)
for i in range(5):
    print(my_die, my_die.current_value)
    my_die.roll()

d_list = [MSDie(6), MSDie(20)]
print(d_list)
# 输出结果如下
<__main__.MSDie object> 6
<__main__.MSDie object> 6
<__main__.MSDie object> 6
<__main__.MSDie object> 6
<__main__.MSDie object> 6
[<__main__.MSDie object>, <__main__.MSDie object>]

This is a nice starting point.
这是一个很好的起点。
In fact, for some assignments this might be all you need.
事实上,对于一些作业,这可能就是你所需要的。
We have a class, we can construct a die, and roll it, and print out the current value.
我们有一个类,我们可以构造一个骰子,滚动它,并打印出当前值。
Sort of… It would be nicer if we could just print(my_die) and have the value of the die show up without having to know about the instance variable called current_value.
如果我们只打印(my_die)并显示die的值,而不需要知道名为current_value的实例变量,那就更好了。

Lets fix up the representation to make printing and interacting with the die a bit more convenient.
让我们修改表示,使打印和与模具交互更方便一些。
For this we will implement the str and repr magic methods.
为此,我们将实现__str__和__repr__魔法方法。

import random

class MSDie:
    """
    Multi-sided die

    Instance Variables:
        current_value
        num_sides

    """

    def __init__(self, num_sides):
        self.num_sides = num_sides
        self.current_value = self.roll()

    def roll(self):
        self.current_value = random.randrange(1,self.num_sides+1)
        return self.current_value

    def __str__(self):
        return str(self.current_value)

    def __repr__(self):
        return "MSDie({}) : {}".format(self.num_sides, self.current_value)


my_die = MSDie(6)
for i in range(5):
    print(my_die)
    my_die.roll()

d_list = [MSDie(6), MSDie(20)]
print(d_list)

# 输出结果如下
3
3
4
4
3
[MSDie(6) : 5, MSDie(20) : 15]

2.2. Making your Class Comparable

2.2. 让你的类具有可比性

(然后这一节好像是空白,莫得内容…)
在这里插入图片描述
2020.12.28

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vicky__3021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值