-1

I have the following code which works fine : this is class parent order1

class order1:

    def __init__(self, type: str, quantity: int) -> None:
        self._type = type
        self._quantity = quantity
        self.members = []

    def __str__(self) -> str:
        return f'{self._type},{self._quantity}'

    def add_order(self, type, quantity) -> str:
        order = order1(type, quantity)
        if type not in self.members:
            self.members.append(order)
        else:
            print('type already exist')
        # return order

    @property
    def type(self) -> str:
        return self._type

    @type.setter
    def type(self, type) -> None:
        self._type = type

    @property
    def quantite(self) -> int:
        return self._quantite

    @quantite.setter
    def quantite(self, quantite) -> None:
        self._quantite = quantite

#item_list.append(item) if item not in item_list else None


if __name__ == '__main__':
    test = order1("nokia22", 1233)

    test2 = test.add_order("nokia22", 123)
print(test)
print(test2)


i have two attributes type of a phone and quantity of same type now i need to create another class to display the inherited attributes from class parent that's what i've done so far

from order import order1
from phone import phone1


class card( order1):
    def __init__(self):
        self.members1 = []

    def __str__(self):
        return '||'.join(str(x) for x in self.members)


if __name__ == '__main__':
    test = card()
    print(test.self.members)

this is the error that i got : NameError: name 'test' is not defined and the expected solution was for the list of orders to be displayed self.members is a list from the class parent

eve
  • 1
  • Move the `print` into the `if __nam__ ...` block – luk2302 Nov 15 '22 at 11:50
  • Does this answer your question? [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – luk2302 Nov 15 '22 at 11:51
  • i'm not sure i competely understood what u meant where should i move the print and why what's the instruction that imma be using instead – eve Nov 15 '22 at 12:04
  • @eve: It needs to be indented within that `if` block, because as written, when the first module is imported (rather than run as a script), you skip over defining `test` and `test2`, but still try to `print` them. Literally add four leading spaces to each of `print(test)` and `print(test2)` in your first module. You need to understand what `if __name__ == '__main__':` is doing, not just copy it into your module due to cargo cult programming. – ShadowRanger Nov 15 '22 at 12:10

1 Answers1

0

Not exactly sure what you're asking because the question seems to be originally about one problem, but when writing your test code you got another problem that was already pointed out (indentation under your if __name__ == '__main__' block).

But you probably had a different question originally about inheritance, beause your card class is bugged. It's not clear why you want to override __init__ because all you're doing is creating some self.members1 = [] attribute that is unused. So you can just delete card.__init__.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • my question is how can i display all phones from class parent in the class child – eve Nov 15 '22 at 12:09
  • Also, in the second module, `test.self.members` makes no sense; the OP just wants `test.members`. – ShadowRanger Nov 15 '22 at 12:11
  • Then if you make the changes I suggested it will work. Plus ditto @ShadowRanger Are you working with a tutorial or guide on how to write classes on python? It might help before tacking a more complex use case to go back to some of the basics (play around with it in the interactive interpreter) and shore up your conceptual understanding. – Iguananaut Nov 15 '22 at 12:13
  • @Iguananaut these are the modifications that i made yet i still getting this error test is not defined – eve Nov 15 '22 at 12:24
  • from order import order1 class card(order1): def __str__(self): return '||'.join(str(x) for x in card.members) if __name__ == '__main__': test = card() print(test.members) – eve Nov 15 '22 at 12:26
  • @eve: Comments can't show indentation, but you're fixing the wrong place. The problem is at the end of the `order` module, not at the end of the module defining `card`. – ShadowRanger Nov 15 '22 at 16:41