Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python __str__ method doesn't translate to go String() method #4

Closed
Nv7-GitHub opened this issue Jul 1, 2021 · 3 comments
Closed

Comments

@Nv7-GitHub
Copy link

Describe the bug
Python classes have an __str__ method that defines how they are printed. Go structs have a String() method that does the same.

Python code example

print("Hello, World!")

class A:
  a = "huh?"
  
  def __init__(self, val):
    self.a = val
  
  def __str__(self):
    return self.a
 
val = A("ok")
print(val)

Current behavior
(relevant code):

func (self *A) __str__() {
	return self.a
}

Expected behavior

func (self *A) String() {
	return self.a
}
@nottheswimmer
Copy link
Owner

nottheswimmer commented Jul 1, 2021

Cool idea! I'll get on it.

EDIT:

For the test case, let's say we want this:

class A:
    a: str

    def __init__(self, val):
        self.a = val

    def __str__(self):
        return self.a


def main():
    val = A("ok")
    print(val)


if __name__ == '__main__':
    main()

To go to this:

package main

import "fmt"

type A struct {
	a string
}

func NewA(val string) (self *A) {
	self = new(A)
	self.a = val
	return
}

func (self *A) String() string {
	return self.a
}

func main() {
	val := NewA("ok")
	fmt.Println(val)
}

@nottheswimmer
Copy link
Owner

Added! Thanks so much for your suggestion, please share if you have any more!

@Nv7-GitHub
Copy link
Author

Thanks for responding so fast! Amazing project!

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

No branches or pull requests

2 participants