python - 这个类看起来很愚蠢吗?

标签 python

<分区>

我正在尝试将我用 PHP 编写的内容移植到 Python 中,主要是为了更好地学习这门语言。有问题的代码是一个 SWF 解析器。在 PHP 中,我将所有数据结构都声明为类。我试图在 Python 中做同样的事情,但似乎没有明确的方法来声明类变量。所以我最终得到了许多看起来像这样的类:

class SWFRGBA(object):
    red = 0
    green = 0
    blue = 0
    alpha = 0

Pythoner 真的会写这样的东西吗?

[编辑]

让我发布一些实际代码来说明这个问题。下面的函数读取 SWF 文件中的矢量形状。 readUB()、readSB() 读取一定数量的位,将它们解释为无符号或有符号。有时,给定字段所需的位数本身就是从比特流中读取的。可能会出现三种类型的记录:直边、二次曲线或样式更改。样式更改记录可能会移动笔的位置、更改线条样式索引、更改两个填充样式索引之一或替换样式数组。

protected function readShapeRecords($numFillBits, $numLineBits, $version, &$bytesAvailable) {
    $records = array();
    while($bytesAvailable) {
        if($this->readUB(1, $bytesAvailable)) {
            // edge
            if($this->readUB(1, $bytesAvailable)) {
                // straight
                $line = new SWFStraightEdge;
                $line->numBits = $this->readUB(4, $bytesAvailable) + 2;
                if($this->readUB(1, $bytesAvailable)) {
                    // general line
                    $line->deltaX = $this->readSB($line->numBits, $bytesAvailable);
                    $line->deltaY = $this->readSB($line->numBits, $bytesAvailable);
                } else {
                    if($this->readUB(1, $bytesAvailable)) {
                        // vertical
                        $line->deltaX = 0;
                        $line->deltaY = $this->readSB($line->numBits, $bytesAvailable);
                    } else {
                        // horizontal 
                        $line->deltaX = $this->readSB($line->numBits, $bytesAvailable);
                        $line->deltaY = 0;
                    }
                }
                $records[] = $line;
            } else {
                // curve
                $curve = new SWFQuadraticCurve;
                $curve->numBits = $this->readUB(4, $bytesAvailable) + 2;
                $curve->controlDeltaX = $this->readSB($curve->numBits, $bytesAvailable);
                $curve->controlDeltaY = $this->readSB($curve->numBits, $bytesAvailable);
                $curve->anchorDeltaX = $this->readSB($curve->numBits, $bytesAvailable);
                $curve->anchorDeltaY = $this->readSB($curve->numBits, $bytesAvailable);
                $records[] = $curve;
            }
        } else {
            $flags = $this->readUB(5, $bytesAvailable);
            if(!$flags) {
                break;
            } else {
                // style change
                $change = new SWFStyleChange;
                if($flags & 0x01) {
                    $change->numMoveBits = $this->readSB(5, $bytesAvailable);
                    $change->moveDeltaX = $this->readSB($change->numMoveBits, $bytesAvailable);
                    $change->moveDeltaY = $this->readSB($change->numMoveBits, $bytesAvailable);
                }
                if($flags & 0x02) {
                    $change->fillStyle0 = $this->readUB($numFillBits, $bytesAvailable);
                }
                if($flags & 0x04) {
                    $change->fillStyle1 = $this->readUB($numFillBits, $bytesAvailable);
                }
                if($flags & 0x08) {
                    $change->lineStyle = $this->readUB($numLineBits, $bytesAvailable);
                }
                if($flags & 0x10) {
                    $change->newFillStyles = $this->readFillStyles($version, $bytesAvailable);
                    $change->newLineStyles = $this->readLineStyles($version, $bytesAvailable);
                    $change->numFillBits = $numFillBits = $this->readUB(4, $bytesAvailable);
                    $change->numLineBits = $numLineBits = $this->readUB(4, $bytesAvailable);
                }
                $records[] = $change;
            }
        }
    }
    $this->alignToByte();
    return $records;
}

最佳答案

如果你只想要拥有一堆属性的实例,你应该使用namedtuple

关于python - 这个类看起来很愚蠢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917459/

相关文章:

python - 程序在 pycharm 中完美运行,在终端中运行时给出错误答案

python - 在 Python 中复制矩阵

Python 安装工具 : install pacakge dependencies from a local repository

python - 如何使用selenium python从自动完成框中提取数据

Python,通过正则表达式找到(n)n字符串的长度

python - 如何在尝试导入 pandas 时修复 "ModuleNotFoundError"

python cmd模块捕获异常后返回提示

python - 必须使用 x 实例作为第一个参数调用未绑定(bind)方法 f() (改为使用 str 实例)

Python3 与 sql 变量

python - 在wxpython中删除图像?