如何将类实例分配给变量并在其他类中使用它

时间:2019-04-23 12:31:43

标签: python object class-variables

我正在为Python做一些基本练习。在这里,我定义了3个类。 现在,我需要在另一个类中传递第一类的实例,并在最后一个类中使用它。

我写了如下代码:

#defining first class:
class MobileInventory:

    def __init__(self, inventory=None):
        if inventory == None:
            balance_inventory = {}
        elif not isinstance(inventory, dict):
            raise TypeError("Input inventory must be a dictionary")
        elif not (set(map(type, inventory)) == {str}):
            raise ValueError("Mobile model name must be a string")
        elif [True for i in inventory.values() if (not isinstance(i, int) or i < 1)]:
            raise ValueError("No. of mobiles must be a positive integer")
        self.balance_inventory = inventory

# class to add elements to existing dictionary of above class
class add_stock:

    def __init__(self, m, new_stock):
        if not isinstance(new_stock, dict):
            raise TypeError("Input stock must be a dictionary")
        elif not (set(map(type, new_stock)) == {str}):
            raise ValueError("Mobile model name must be a string")
        elif [True for i in new_stock.values() if (not isinstance(i, int) or i < 1)]:
            raise ValueError("No. of mobiles must be a positive integer")

        for key, value in new_stock.items():
            if key in m.balance_inventory.keys():
                x = m.balance_inventory[key] + value
                m.balance_inventory[key] = x
            else:
                m.balance_inventory.update({key: value})

#class to testing the above functionality
class Test_Inventory_Add_Stock:

    m = ''

    def setup_class():
        m = MobileInventory({'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25})
        print(m.balance_inventory)  # giving Output {'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25}

    def test_add_new_stock_as_dict():
        add_stock( m, {'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10})

Test_Inventory_Add_Stock.setup_class()
Test_Inventory_Add_Stock.test_add_new_stock_as_dict()

上面我为test_add_new_stock_as_dict方法给出了错误“ NameError:名称'm”未定义。

为什么我在课堂上宣布时不带m? 如何在add_stock类中直接使用MobileInventory.balance_inventory?我试过它给了错误。

预期: 我需要删除NameError。 还有直接在类中使用MobileInventory.balance_inventory(即另一个类引用)的任何方法,而无需使用该方法的实例

5 个答案:

答案 0 :(得分:0)

Python变量名作用域比本地作用域更喜欢局部作用域,因此您需要告诉解释器m的来源。

在第一种和第二种方法中,您都可以使用Test_Inventory_Add_Stock.m来引用您的静态类变量m

class Test_Inventory_Add_Stock:

    m = ''

    def setup_class():
        Test_Inventory_Add_Stock.m = MobileInventory({'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25})
        print(m.balance_inventory)  # giving Output {'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25}

    def test_add_new_stock_as_dict():
        add_stock(Test_Inventory_Add_Stock.m, {'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10})

但这看起来不太好。要将变量限制在类的实例中,请尝试以下操作:

class Test_Inventory_Add_Stock:

    def setup_class(self):
        self.m = MobileInventory({'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25})
        print(m.balance_inventory)  # giving Output {'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25}

    def test_add_new_stock_as_dict(self):
        add_stock(self.m, {'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10})

t = Test_Inventory_Add_Stock()
t.setup_class()
t.test_add_new_stock_as_dict()

答案 1 :(得分:0)

并为此进行测试,请使用此库 从proj.inventory导入MobileInventory,InsufficientException 导入pytest

TestingInventoryCreation()类:

def test_creating_empty_inventory(self):
    c1 = MobileInventory()
    assert c1.balance_inventory == {}

def test_creating_specified_inventory(self):
    c2 = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})
    assert c2.balance_inventory == {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}
    #{'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}

def test_creating_inventory_with_list(self):
    #c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])
    with pytest.raises(TypeError) :
        c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])


def test_creating_inventory_with_numeric_keys(self):
    #c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia Model Z'})
    with pytest.raises(ValueError):
        c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia Model Z'})


def test_creating_inventory_with_nonnumeric_values(self):
    #c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000', 'Nokia Model Z':'25'})
    with pytest.raises(ValueError):
        c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000', 'Nokia Model Z':'25'})


def test_creating_inventory_with_negative_value(self):
    #c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia Model Z':25})
    with pytest.raises(ValueError) :
        c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia Model Z':25})

TestInventoryAddStock()类: 库存=无

    @classmethod
    def setup_class(cls):
            cls.inventory = MobileInventory({'iPhone Model X': 100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})

    def test_add_new_stock_as_dict(self):
            self.inventory.add_stock({'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10})
            assert self.inventory.balance_inventory == {'iPhone Model X': 150, 'Xiaomi Model Y': 3000, 'Nokia Model Z': 25, 'Nokia Model A': 10}


    def test_add_new_stock_as_list(self):
            with pytest.raises(TypeError) :
                    MobileInventory.add_stock(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])


    def test_add_new_stock_with_numeric_keys(self):
            with pytest.raises(ValueError):
                    MobileInventory.add_stock({1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'})


    def test_add_new_stock_with_nonnumeric_values(self):
            with pytest.raises(ValueError):
                    MobileInventory.add_stock({'iPhone Model A':'50', 'Xiaomi Model B': '2000', 'Nokia ModelC':'25'})


    def test_add_new_stock_with_float_values(self):
            with pytest.raises(ValueError):
                    MobileInventory.add_stock({'iPhone Model A':50.5, 'Xiaomi Model B':2000.3, 'Nokia Model C':25})

Class TestInventorySellStock(): 库存=无

@classmethod
def setup_class(cls):
    cls.inventory = MobileInventory(
        {'iPhone Model A': 50, 'Xiaomi Model B': 2000, 'Nokia Model C': 10, 'Sony Model D': 1})

def test_sell_stock_as_dict(self):
    self.inventory.sell_stock({'iPhone Model A': 2, 'Xiaomi Model B': 20, 'Sony Model D': 1})
    assert self.inventory.balance_inventory == {'iPhone Model A': 48, 'Xiaomi Model B': 1980, 'Nokia Model C': 10,
                                                'Sony Model D': 0}

def test_sell_stock_as_list(self):
    with pytest.raises(TypeError):
        MobileInventory.sell_stock(['iPhone Model A', 'Xiaomi Model B', 'Nokia Model C'])

def test_sell_stock_with_numeric_keys(self):
    with pytest.raises(ValueError):
        MobileInventory.sell_stock({1: 'iPhone Model A', 2: 'Xiaomi Model B', 3: 'Nokia Model C'})

def test_sell_stock_with_nonnumeric_values(self):
    with pytest.raises(ValueError):
        MobileInventory.sell_stock({'iPhone Model A': '5', 'Xiaomi Model B': '3', 'Nokia Model C': '4'})

def test_sell_stock_with_float_values(self):
    with pytest.raises(ValueError):
        MobileInventory.sell_stock({'iPhone Model A': 2.5, 'Xiaomi Model B': 3.1, 'Nokia Model C': 4})

def test_sell_stock_of_nonexisting_model(self):
    with pytest.raises(InsufficientException):
        MobileInventory.sell_stock({'iPhone Model B': 2, 'Xiaomi Model B': 5})

def test_sell_stock_of_insufficient_stock(self):
    with pytest.raises(InsufficientException):
        MobileInventory.sell_stock({'iPhone Model A': 2, 'Xiaomi Model B': 5, 'Nokia Model C': 15})

答案 2 :(得分:0)

def test_sell_stock_of_nonexisting_model(self):
    with pytest.raises(InsufficientException):
        MobileInventory.sell_stock({'iPhone Model B': 2, 'Xiaomi Model B': 5})

def test_sell_stock_of_insufficient_stock(self):
    with pytest.raises(InsufficientException):
        MobileInventory.sell_stock({'iPhone Model A': 2, 'Xiaomi Model B': 5, 'Nokia Model C': 15})

TypeError:sell_stock()缺少1个必需的位置参数:“ requested_stock”

答案 3 :(得分:0)

我猜下面是在运行 Pytests 时定义类属性的预期方式。

class TestInventoryAddStock:
    @classmethod
    def setup_class(cls):
        cls.inventory = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})

    
    def test_add_new_stock_as_dict(self):
        self.inventory.add_stock({'iPhone Model X':50, 'Xiaomi Model Y': 2000, 'Nokia Model A':10})
        assert self.inventory.balance_inventory == {'iPhone Model X':150, 'Xiaomi Model Y': 3000, 'Nokia Model Z':25, 'Nokia Model A':10}

答案 4 :(得分:-1)

class InsufficientException(Exception):
    pass


class MobileInventory:
    balance_inventory = {}
    def __init__(self, inventory=None):
        if not inventory:
            MobileInventory.balance_inventory = {}
        else:
            if not isinstance(inventory, dict):
                raise TypeError("Input inventory must be a dictionary")
            elif not (set(map(type, inventory)) == {str}):
                raise ValueError("Mobile model name must be a string")
            else:
                for i in inventory.values():
                    if isinstance(i, int) and i > 0:
                        pass
                    else:
                        raise ValueError("No. of mobiles must be a positive integer")
            MobileInventory.balance_inventory = inventory

@classmethod
def add_stock(cls, new_stock):
    print("before update")
    print(cls.balance_inventory)
    if not isinstance(new_stock, dict):
        raise TypeError("Input stock must be a dictionary")
    elif not (set(map(type, new_stock)) == {str}):
        raise ValueError("Mobile model name must be a string")
    else:
        for i in new_stock.values():
            if isinstance(i, int) and i > 0:
                pass
            else:
                raise ValueError("No. of mobiles must be a positive integer")

    for key, value in new_stock.items():
        if key in cls.balance_inventory.keys():
            x = cls.balance_inventory[key] + value
            cls.balance_inventory[key] = x
        else:
            cls.balance_inventory.update({key: value})
    print(new_stock)
    print("after update")
    print(cls.balance_inventory)


@classmethod
def sell_stock(cls, requested_stock):
    if not isinstance(requested_stock, dict):
        raise TypeError("Requested stock must be a dictionary")
    elif not (set(map(type, requested_stock)) == {str}):
        raise ValueError("Mobile model name must be a string")
    else:
        for i in requested_stock.values():
            if isinstance(i, int) and i > 0:
                pass
            else:
                raise ValueError("No. of mobiles must be a positive integer")
    for key, value in requested_stock.items():
        if key in cls.balance_inventory.keys():
            if cls.balance_inventory[key] < value:
                raise InsufficientException("Insufficient Stock")
            else:
                y = cls.balance_inventory[key] - value
                cls.balance_inventory[key] = y
        else:
            raise InsufficientException("No Stock. New Model Request")