1

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
setevoy
  • 4,374
  • 11
  • 50
  • 87

2 Answers2

1

You shouldn't create properties of a class on-the-fly in its method. A class should know which properties it inherited from the super class upon initialization. You should create instance variables only in the __init__, a special constructor method.

class First(object):
    def __init__(self, first_arg):
        self.first_arg = first_arg

    @property
    def one(self):
        return self.first_arg

    @one.setter
    def one(self, value):
        self.first_arg = value

>>> first = First(5)
>>> print first.one
5
>>> first.one = 10
>>> print first.one
10

If you want to add an extra property to First class by creating a new class called Second, you should always first inherit the super class's properties in the subclass's constructor:

class Second(First):
    def __init__(self, first_arg, second_arg):
        super(Second, self).__init__(first_arg) # now you have "self.first_arg"
        self.second_arg = second_arg

    @property
    def two(self):
        return self.second_arg

    @two.setter
    def two(self, value):
        self.second_arg = value

>>> second = Second(7, 10)
>>> print second.one
7
>>> print second.two
10
>>> second.two = 20
>>> second.one = 15
...

Hope this helps.

Community
  • 1
  • 1
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
0

I will quickly name your problems in this code:

Your naming variables go like one, two, cl1, cl2. Those are senseless and are HARD to understand, especially for beginners like you. It is way easier for you to think of some real world example and try to build a structure for that.

Personally, I always create classes about games which I play, so that way everything seems logical and easy to learn for me.

Example: You are playing Call of Duty. Then you would have the following classes: Soldier, Civilian, Weapon, Map and so on. Soldier would have methods:

  • Soldier.Run()
  • Soldier.Shoot()
  • Soldier.ReloadWeapon()

And AmericanSoldier and GermanSoldier would all inherit the base Soldier class and it is all logical (and fun) to code!

I am yet to learn a new programming language using the moronic exampless of autist guides with "Var_1, Var_2" which a part of "Class1".

Placeholder
  • 4,651
  • 6
  • 33
  • 35
  • 1
    Thanks. I'm always try to give "significant" names for vars in my code. Here is such names just because this was "quic example" code. But you are absolutely right. – setevoy Feb 21 '15 at 12:30