From 18fb6da5ada320ef1b80201cd6bb4304e20bdf36 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 10 Dec 2023 10:39:45 -0600 Subject: [PATCH] ex 6.3 --- stock.py | 5 +++-- structure.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/stock.py b/stock.py index b97a987..bf69a82 100644 --- a/stock.py +++ b/stock.py @@ -2,8 +2,6 @@ from structure import Structure class Stock(Structure): - _fields = ('name', 'shares', 'price') - def __init__(self, name, shares, price): self._init() @@ -13,3 +11,6 @@ class Stock(Structure): def sell(self, nshares): self.shares -= nshares + + +Stock.set_fields() diff --git a/structure.py b/structure.py index 8ece04c..5eae3ee 100644 --- a/structure.py +++ b/structure.py @@ -1,3 +1,4 @@ +import inspect import sys @@ -20,3 +21,8 @@ class Structure: super().__setattr__(name, value) else: raise AttributeError(f"No attribute {name}") + + @classmethod + def set_fields(cls): + sig = inspect.signature(cls) + cls._fields = tuple(sig.parameters)