gpython icon indicating copy to clipboard operation
gpython copied to clipboard

[Question] How to implement object methods?

Open bbrodriges opened this issue 7 years ago • 2 comments

I want to try to implement hashlib (PR will follow).

For now I have implemented Type, module, module method and some "magic methods", but I cannot figure out how to implement object methods such as update, hexdigest etc.

Here is my WIP implementation.

Thanks in advance.

bbrodriges avatar Aug 23 '18 17:08 bbrodriges

This isn't particularly elegant at the moment, but see py/generator.go for an example. You basically add the method objects to the type's Dict by hand.

	GeneratorType.Dict["send"] = MustNewMethod("send", func(self Object, value Object) (Object, error) {
		return self.(*Generator).Send(value)
	}, 0, "send(arg) -> send 'arg' into generator,\nreturn next yielded value or raise StopIteration.")

ncw avatar Aug 28 '18 16:08 ncw

recommend @sbinet close -- addressed w/ https://github.com/go-python/gpython/pull/163

drew-512 avatar Feb 22 '22 18:02 drew-512