Do you think declarations within Python class are equivalent to those within __init__ method?


Declarations anywhere in the class(other than in __init__) and declarations in the __init__method are not the same. The following code shows this to be true.

Example

import sys
class foo():
    print 'within class'
    def __init__(self):
        print 'within init'
    def do_smthng(self):
        print 'do something'

def main():
    f=foo()
    f.do_smthng()
    return 0
if __name__ == '__main__':
    sys.exit( main() )

Output

within class
within init
do something

Updated on: 16-Jun-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements