I just start experimenting with OOP in Python 2.7.
Here is example of code, which I currently writing for my project. This is just 'template' - not real code fron app, but this app will use same classes "scheme", as in example below.
I'd like to know - what I'm doing incorrect here? Classes initialization, some wrong inheritance usage, other mistakes?
It works, but - may be I'm wrong with some 'ideological' points?
Files: main.py
- programm body; cl1.py
- First class; cl2.py
- Second class.
main.py
:
#!/usr/bin/env python
import cl1, cl2
print('\nStarted Main script.\n')
cl1 = cl1.First()
cl1.one('This is first_arg')
cl2 = cl2.Second()
cl2.two('This is second arg')
cl1.py
:
class First:
def one(self, first_arg):
self.third_arg = 'This is third arg from First class'
self.first_arg = first_arg
print('This is method \'one\' from class First.\nWork with first_arg = %s\n' % self.first_arg)
cl2.py
:
from cl1 import First
class Second(First):
def two(self, second_arg):
self.second_arg = second_arg
print('This is method \'two\' from class Second.\nWork with second_arg = %s\n' % self.second_arg)
self.one('I came from Second.two() to First.one()')
print('I came from First.one() as self.third_arg to Second.two(): %s\n' % self.third_arg)
Result:
$ ./main.py
Started Main script.
This is method 'one' from class First.
Work with first_arg = This is first_arg
This is method 'two' from class Second.
Work with second_arg = This is second arg
This is method 'one' from class First.
Work with first_arg = I came from Second.two() to First.one()
I came from First.one() as self.third_arg to Second.two(): This is third arg from First class