我应该如何在python中向int添加方法? [英] Should I, and how to, add methods to int in python?

查看:216
本文介绍了我应该如何在python中向int添加方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是一个与学习相关的问题,所以我为什么要做或不做任何事情的建议,以及推荐的资源来使这些事情变得更好,不仅仅是好的。)

(This is a learning related question, so any advice in why should I do or not do anything, as well as recommended resources to get these things better, are more than wellcome.)

我正在尝试在python中学习OOP,并做了一个简单的Time类,如下所示:

I'm trying to learn about OOP in python, and have done a simple "Time" class like this:

class Time(object):

    """A time representation."""

    def __init__(self, hours=0, minutes=0, seconds=0):
        self.hours = hours
        self.minutes = minutes
        self.seconds = seconds

    def __int__(self):
        return self.hours * 3600 + self.minutes * 60 + self.seconds

现在, __ int __ 方法让我从Time实例中获取一个int,表示我执行 int(时间(3,4)时的时间(但以秒为单位) ,5))。顺便说一下,我觉得这很棒。但是,为了使用它,知道如何让int有一个返回Time对象的新方法会很好,所以像 3643.time()可以完成。

Now, that __int__ method lets me get an int from my "Time" instances that represent the time (but in seconds) when I do int(Time(3, 4, 5)). I find that to be kind of awesome, by the way. But, in order to use this, it would be nice to know how to make an "int" have a new method that returns a "Time" object, so something like 3643.time() could be done.

为什么?
出于各种原因:

Why? For various reasons:


  1. 因为这样我现在才知道对我来说是什么样的黑魔法。 (这些都强调了让事情发生的事情......以及相关的东西)

  1. Because that way I learn about what for me is like black magic right now. (these underscored things that make things happen... and related stuff)

因为我不知道为什么我不应该这样做。 (请告诉我)

Because I don't know why I shouldn't. (so tell me please)

我想我可以在任何课程之外做以下的事情:

I suppose I could do something like the following outside any class:

def time(seconds):
    """Return an HH:MM:SS stamp of "seconds", where "seconds" should be an int."""
    hours, minutes = 0, 0
    if seconds >= 3600:
        hours, seconds = divmod(int, 3600)
    if seconds >= 60:
        minutes, seconds = divmod(int, 60)
    return "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)

但这似乎与这些对象无关,它似乎没有那么多面向对象...所以,我认为可能有更好的方法来处理这种事情。

But that doesn't seem to relate to these objects enough, It doesn't seem that much object oriented... So, I reckon maybe there is a better way to approach this kind of thing.

推荐答案

不,你不能真的将方法添加到int。你可以继承int,并从你的 __ int __ 方法返回它,但这对你没什么帮助,因为调用 int() on仍然会返回一个常规的int。

No, you cannot really add methods to int. You could subclass int, and return that from your __int__ method, but that won't help you much, since calling int() on that will still return a regular int.

你想使用常规函数转换回时间()是正确的,但它可能很好,使它成为一个类方法,所以它更明显的相关性:

Your idea of using a regular function to convert back to Time() is right, but it might be nice to make it a classmethod so it's a little more obvious how it relates:

class Time(object):
    @classmethod
    def from_seconds(cls, seconds):
         _hours, _minutes, _seconds = ...
         return cls(_hours, _minutes, _seconds)

这篇关于我应该如何在python中向int添加方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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