Skip to content

Changed Array2D from_string constructor to be classmethod#806

Open
HarrisonTCodes wants to merge 1 commit into
pokepetter:masterfrom
HarrisonTCodes:fix/array-2d-from-string
Open

Changed Array2D from_string constructor to be classmethod#806
HarrisonTCodes wants to merge 1 commit into
pokepetter:masterfrom
HarrisonTCodes:fix/array-2d-from-string

Conversation

@HarrisonTCodes
Copy link
Copy Markdown
Contributor

Changed Array2D from_string constructor / factory method, which was previously a staticmethod, to be a classmethod.

This is inline with common semantics of constructor / factory methods being class methods. It also fixes previously broken usage of this method with inheritance and child classes.

Previously, this code:

class Test(Array2D):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    def test(self):
        print("test")

t = Test.from_string("0,1,0\n0,0,1\n0,0,0")
t.test()

Would error, as the from_string static method always returns an instance of the parent class, and t would be of type Array2D instead of the child Test class, hence the test method would not exist on it. With the use of a class method instead, t = Test.from_string(...) returns an instance of the child class (Test) correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant