class implementation

Dave Angel davea at davea.name
Tue Oct 8 06:31:20 EDT 2013


On 8/10/2013 04:20, markotaht at gmail.com wrote:

> I cant just subclassing doesent work.

I can't parse that "sentence."

> It seem the init method of the source class also calls out another
class. And the problem is, i can subclass the other class to with the required function but the end result is that it doesent work, since the source class cant accsess the subclass functions. 

What's a "source class"?  If you mean parent class, then say so.
Otherwise, if you give it a name, we might be able to follow.   But
"source" and "other" don't narrow the field very much.

A parent class can certainly access the child class (subclass) 
methods (not functions).  But only if the instance (self) is an instance
of the child class. That's the whole point of subclassing.

>
> The source code is pykkar. 
>
> https://courses.cs.ut.ee/all/MTAT.03.100/2012_fall/uploads/opik/_downloads/pykkar.py
>
> I want to add it a new ability called left(). I cant manipulate the source class, cause then my comp will be the only one where the program runs.
>
> class pykkar_l(Pykkar):
>     def left(self):
>         self._world.execute("left")
>
>     def _cmd_left(self):
>         headings = (N,E,S,W)
>         cur_tile = self._get_current_tile() 
>         
>         cur_heading_index = headings.index(cur_tile.pykkar_heading)
>         new_heading_index = (cur_heading_index - 1) % 4
>         cur_tile.pykkar_heading = headings[new_heading_index]
>         
>         self._update_pykkar_image(cur_tile)
>
> class world_l(World):
>     def left(self):
>         self._world.execute("left")
>
> These are my subclasses. For it to work. Class World, must obtain the method from subclass world_l

Then it sounds like you should make sure that the global value "world"
in that module is an instance of your world_l class, rather than an
instance or World.  And that the proxy is an instance of pykkar_l rather
than of Pykkar.

import pykkar

layout = "fdlkjdsljdslfkjsdljfdsf"
pykkar.world = world_I(layout)
??? = pykkar_l(pykkar.world)

You don't show your own top-level code, so I can't integrate it in.

By the way, it's conventional to use uppercase for class names, and
lowercase for instances of those classes.

I'm astounded that your class is using eval and multiprocessing before
understanding classes and subclasses.


-- 
DaveA





More information about the Python-list mailing list