1

Okay so I have the following:

Class OwnableObject(MobileObject):
   def __init__(self, name):
      MobileObject.__init__(self, name)
      self.owner = None # not owned

   def is_ownable(self): return True
   def is_owned(self): return self.owner

What is the difference between invoking the is_ownable method on OwnableObject
and invoking the is_ownable method on a MobileObject.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
Scheme 2020
  • 111
  • 1
  • 4
  • 1
    I pressume that is a general programming question, not Python-specific. You should read about classes and inheritance in some text on object-oriented programming. – Genba Mar 25 '11 at 14:31
  • 123, see my edited answer below. Since there doesn't appear to be a definition of `is_ownable` for `MobileObject` here, you can't do this: `mo = MobileObject(); mo.is_ownable()`. The result would be an error. My answer shows how to do what you're describing. – senderle Mar 25 '11 at 15:20

3 Answers3

3

Update: Based on the code you have posted now, it's impossible to call is_ownable on a MobileObject because the MobileObject doesn't appear to have a definition for is_ownable.

If it does, then the difference is simply the difference between MobileObject's definition and OwnableObject's definition. I've updated the terms of the below to illustrate what I mean.

If you create a class in Python (or in any language, really):

class MobileObject(object):
    def __init__(self, position):
        self.position = position
    def move(self, position):
        self.position = position
    def is_ownable(self):
        return False

And then create a subclass:

class OwnableObject(MobileObject):
    def __init__(self, position, owner=None):
        MobileObject.__init__(self, position)
        self.owner = owner
    def is_ownable(self):
        return True
    def is_owned(self):
        return self.owner

The resulting subclass automatically inherits the methods of its superclass:

movable = MobileObject()
movable.is_ownable()       # returns False
movable.move(new_position) # moves movable
movable.is_owned()         # causes an error

ownable = OwnableObject()
ownable.is_ownable()       # returns True
ownable.move(new_position) # moves ownable
movable.is_owned()         # returns owner or None

As you can see, is_ownable() and is_owned() differ between the two classes -- and in the latter case, since is_owned() is not defined, it causes an error when called on movable. But move() works identically in both classes.

senderle
  • 145,869
  • 36
  • 209
  • 233
  • 1
    Posting multi-line code in a comment box doesn't really work, so I can't quite tell what you're asking. Could you edit your original question? – senderle Mar 25 '11 at 14:46
  • @Python123: Please edit your question if you have more to add. Unformated Python code without the line breaks and spaced can change everything. – unholysampler Mar 25 '11 at 14:47
1

I suppose that it means the same thing as in any programming language that supports the object oriented paradigm:

>>> class Base:
...     def ok(self):
...         print 'OK'
... 
>>> class SubClass(Base):
...     def oops(self):
...         print 'Oops'
... 
>>> x = SubClass()
>>> x.ok()
OK
>>> 
thkala
  • 84,049
  • 23
  • 157
  • 201
1

All of the methods implemented in the base class can be called on the subclass. The base implementation will be used unless you override the method in the subclass.

unholysampler
  • 17,141
  • 7
  • 47
  • 64