Python Forum
calling on a method from one class into another class which is not a child
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling on a method from one class into another class which is not a child
#6
If getMaxX was a class method it would have to return a class variable.
class World:
    maxX = 100
    maxY = 100

    @classmethod
    def set_range(cls, mx, my)
        cls.maxX = mx
        cls.maxY = my


class Fish(LifeForm):
    def liveALittle(self)
        ...
        if 0 <= newX <= World.maxX:


def main():
    """Create everything"""
    World.setRange(10,000, 50,000)

    red_fish = Fish()
World could be a "class only" type class (My TM if nobody else has uses this), or it could be a regular class with some class attributes that are common to all instances.

If this pattern does not work for, you'll need to pass an instance of World to Fish. If all Fish are in the same World, the World attribute in Fish could be a class attribute.
class World:
    def __init__(self, mx, my):
        self.maxX = mx
        self.maxY = my


class Fish(LifeForm):
    world = None

    @classmethod
    def set_world(cls, world):
        cls.world = world

    def liveALittle(self)
        ...
        if 0 <= newX <= self.world.maxX:


def main():
    """Create everything"""
    world = World(10,000, 50,000)

    Fish.set_world(world)
    red_fish = Fish()
Reply


Messages In This Thread
RE: calling on a method from one class into another class which is not a child - by deanhystad - Apr-29-2020, 07:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 163 Today, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 154 Today, 04:04 AM
Last Post: deanhystad
  [split] Class and methods ebn852_pan 2 147 Yesterday, 08:07 AM
Last Post: Gribouillis
  How does this code create a class? Pedroski55 6 571 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 316 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 350 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 448 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 617 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 793 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 839 Feb-04-2024, 09:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020