how to assign Class Instance to a variable and use that in other class

半腔热情 提交于 2020-06-26 12:23:07

问题


I am doing some basic practice for Python. Here, I am defining 3 classes. Now, I need to pass instance of first class in another class and use that in the last one.

I wrote code like below:

#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()

The above i giving error 'NameError: name 'm' is not defined' for test_add_new_stock_as_dict method.

Why is it not taking m, when I am declaring that in class? how is it possible to use MobileInventory.balance_inventory directly in add_stock class? I tried it's giving error.

Expected: I need to remove the NameError. And any way to use MobileInventory.balance_inventory (i.e. another class reference) directly in class without instance of that


回答1:


Python variable name scopes prefer the local scope over anything outside, so you need to tell the interpreter where m is coming from.

Both in the first and second method, you can use Test_Inventory_Add_Stock.m to refer to your static class variable 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})

But that doesn't look very good. To keep the variables confined to an instance of the class, try this:

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()




回答2:


AND for Testing this use this library from proj.inventory import MobileInventory, InsufficientException import pytest

class 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})

class TestInventoryAddStock(): inventory = None

    @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(): inventory = None

@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})



回答3:


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")


来源:https://stackoverflow.com/questions/55811299/how-to-assign-class-instance-to-a-variable-and-use-that-in-other-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!