相关疑难解决方法(0)

为什么我不能将datetime.date子类化?

以下为什么不工作(Python 2.5.2)?

>>> import datetime
>>> class D(datetime.date):
        def __init__(self, year):
            datetime.date.__init__(self, year, 1, 1)
>>> D(2008)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 3 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

我想创建一个类似datetime.date但具有不同__init__功能的类.显然我的功能永远不会被调用.相反,原来datetime.date.__init__被调用并失败,因为这需要3个参数,而我正在传递一个.

这里发生了什么?这是一个线索吗?

>>> datetime.date.__init__
<slot wrapper '__init__' of 'object' objects>
Run Code Online (Sandbox Code Playgroud)

谢谢!

python oop datetime subclass

19
推荐指数
2
解决办法
5888
查看次数

在Python中扩展基类

我正在尝试在Python中扩展一些"基础"类:

class xlist (list):
    def len(self):
        return len(self)

    def add(self, *args):
        self.extend(args)
        return None


class xint (int):
    def add(self, value):
        self += value
        return self


x = xlist([1,2,3])
print x.len()   ## >>> 3 ok
print x         ## >>> [1,2,3] ok
x.add (4, 5, 6)
print x         ## >>> [1,2,3,4,5,6] ok

x = xint(10)
print x         ## >>> 10 ok
x.add (2)
print x         ## >>> 10  # Not ok (#1)

print type(x)         ## >>> <class '__main__.xint'> ok
x += 5 …
Run Code Online (Sandbox Code Playgroud)

python

11
推荐指数
2
解决办法
2万
查看次数

如何计算galois域上的numpy数组?

我想在galois字段(GF4)上使用numpy数组.所以,我将GF4类设置为数组元素.它适用于数组+整数计算,但它不适用于数组+数组计算.

import numpy

class GF4(object):
    """class for galois field"""
    def __init__(self, number):
        self.number = number
        self.__addL__ = ((0,1,2,3),(1,0,3,2),(2,3,0,1),(3,2,1,0))
        self.__mulL__ = ((0,0,0,0),(0,1,2,3),(0,2,3,1),(0,3,1,2))
    def __add__(self, x):
        return self.__addL__[self.number][x]
    def __mul__(self, x):
        return self.__mulL__[self.number][x]
    def __sub__(self, x):
        return self.__addL__[self.number][x]
    def __div__(self, x):
        return self.__mulL__[self.number][x]
    def __repr__(self):
        return str(self.number)

a = numpy.array([GF4(numpy.random.randint(4)) for i in range(18)]).reshape(3,6)
b = numpy.array([GF4(numpy.random.randint(4)) for i in range(18)]).reshape(3,6)

""""
In [261]: a
Out[261]: 
array([[1, 1, 2, 0, 2, 1],
       [0, 3, 1, 0, 3, 1],
       [1, 2, 0, 3, …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy galois-field

5
推荐指数
1
解决办法
2164
查看次数

对int进行子类化并重写__init__方法 - Python

可能重复:
从str或int继承

嗨伙计,

我试图继承int类没有任何成功.这是我的尝试:

class SpecialInt(int):
    def __init__(self, x, base=10, important_text=''):
        int.__init__(self, x, base)
        self.important_text=important_text
Run Code Online (Sandbox Code Playgroud)

如果我执行以下操作:

integer = SpecialInt(123, 10, 'rage of the unicorns')
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TypeRror: int() takes at most 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?:)

python int built-in subclassing

3
推荐指数
1
解决办法
3132
查看次数

标签 统计

python ×4

arrays ×1

built-in ×1

datetime ×1

galois-field ×1

int ×1

numpy ×1

oop ×1

subclass ×1

subclassing ×1