Home Python class binding not working, throws errors
Reply: 1

Python class binding not working, throws errors

slackexchange
1#
slackexchange Published in 2018-01-13 09:35:40Z

So I've been watching a video tutorial on classes in Python, and the code looks like this:

class MyPythonClass():

    def _init_(self, msg):
        self.msg = msg
        self.print_msg()

    def print_msg(self):
        print(self.msg)


inst = MyPythonClass('hello')

That's copied pretty much verbatim, and the code works in the video. But when using it as above in Eclipse workspace, i get the following error:

TypeError: object() takes no parameters

And, when using it with the class method print_msg('hello') instead, I get the following error:

TypeError: say_hello() takes 1 positional argument but 2 were given

I only passed 1 string argument, not 2 arguments, right..

Btw, the tutorial I think uses around Python 3.1, while I'm using Python 3.6.

Why do I get these errors?

holdenweb
2#
holdenweb Reply to 2018-01-13 09:40:17Z

Read __init__ as "dunder init" and you'll never forget that the special method names begin and end with two underlines!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.368939 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO