小编bal*_*pha的帖子

访问实例属性和类属性之间的区别

我有一个Python类

class pytest:
    i = 34
    def func(self):
        return "hello world"
Run Code Online (Sandbox Code Playgroud)

当我访问时pytest.i,我得到34.我也可以这样做:

a = pytest()
a.i
Run Code Online (Sandbox Code Playgroud)

这也给了34.

如果我试图访问(不存在)pytest.j,我得到

Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
pytest.j
AttributeError: class pytest has no attribute 'j'
Run Code Online (Sandbox Code Playgroud)

当我尝试时a.j,错误是

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
a.j
AttributeError: pytest instance has no attribute 'j'
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:两种情况到底发生了什么,有什么区别?

python class instance

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

扩展现有API:使用默认参数或包装函数?

我有一个现有的方法(或一般的功能),我需要增加其他功能,但我不想在代码中的其他地方使用该方法.例:

int foo::bar(int x)
{
 // a whole lot of code here
 return 2 * x + 4;
}
Run Code Online (Sandbox Code Playgroud)

广泛用于代码库.现在我需要将4转换为参数,但任何已经调用foo :: bar的代码仍然应该接收它所期望的内容.我应该扩展并重命名旧方法并将其包装成新的方法

int foo::extended_bar(int x, int y)
{
 // ...
 return 2 * x + y;
}

int foo::bar(int x)
{
 return extended_bar(x,4);
}
Run Code Online (Sandbox Code Playgroud)

或者我应该在头文件中声明一个默认参数,如

int bar(int x, int y=4);
Run Code Online (Sandbox Code Playgroud)

并只是扩展功能

int foo::bar(int x, int y)
{
 // ...
 return 2 * x + y;
}
Run Code Online (Sandbox Code Playgroud)

每种变体有哪些优缺点?

c++ backwards-compatibility

2
推荐指数
1
解决办法
550
查看次数

python断言函数的建议

我在多个脚本中多次使用断言,我想知道是否有人对更好的方法有任何建议,而不是我在下面创建的函数.

def assert_validation(expected, actual, type='', message=''):

    if type == '==':

        assert expected == actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)

    elif type == '!=':

        assert expected != actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)

    elif type == '<=':

        assert expected <= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)

    elif type == '>=':

        assert expected >= actual, 'Expected: %s, Actual: %s, %s' %(expected, actual, message)



def assert_str_validation(expected, actual, type='', message=''):

    if type == '==':

        assert …
Run Code Online (Sandbox Code Playgroud)

python assert

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

初始化很多字段 - 构造函数vs方法返回值

我目前正在开发Java交易卡游戏,类似于旧的神奇宝贝游戏.我现在要做的是以某种方式定义所有卡片,但因为有很多字段需要初始化,我正在考虑其他方法,因为构造函数将非常长并且每张卡片都难以读取.我还必须初始化攻击,这意味着每次我必须基本上创建一个匿名内部类(这个术语是正确的吗?),如下所示:

/**
 * Base set Abra 43/102
 */
public final class Abra extends Pokemon 
{

    public Abra() 
    {
        super(
                new ImageIcon("img/scans/base-set/43-abra.jpg"), 
                "Abra", 
                "Base Set Abra",
                null, 
                Type.PSYCHIC, 
                Type.PSYCHIC, 
                Type.NONE, 
                30, 
                0
        );

        attack1 = new Attack("Psyshock", Type.NORMAL) 
        {

            /**
             * 10 damage. Flip a coin. If heads, the Defending Pokémon is now Paralyzed.
             */
            public void doAttack() 
            {
                damageApplyWeaknessAndResistance(10);
                if (gui.frames.CoinFlipDialog.showCoinFlipFrame() == CoinFlip.COIN_HEADS) 
                {
                    Game.getOpponentPlayer().getActivePokemon().status = Status.Paralyzed;
                }
            }
        };

        attack2 = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的第二个选择是使用接口和抽象类创建一个层次结构,这意味着值不会存储在字段中,而是在需要时由方法返回:

public interface Card extends …
Run Code Online (Sandbox Code Playgroud)

java constructor field

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