python - If a function (that's outside of the class) needs to use a method from a class, how do I call the method? -


here's class:

class minernotfullaction:    def __init__(self, entity, image_store):       self.entity = entity       self.image_store = image_store     def miner_to_ore(self, world, ore):       entity_pt = entities.get_position(self.entity)       if not ore:          return ([entity_pt], false)       ore_pt = entities.get_position(ore)       obj = point.point(0, 0)       if obj.adjacent(entity_pt, ore_pt):          entities.set_resource_count(self.entity,             1 + entities.get_resource_count(self.entity))          remove_entity(world, ore)          return ([ore_pt], true)       else:          new_pt = next_position(world, entity_pt, ore_pt)          return (worldmodel.move_entity(world, entity, new_pt), false) 

and here's function that's in same file, it's outside of class:

def miner_not_full_action(world, action, ticks):    entity = action.entity    entity_pt = entities.get_position(entity)    ore = find_nearest(world, entity_pt, entities.ore)    (tiles, found) = minernotfullaction.miner_to_ore(world, entity, ore)     if found:       entity = try_transform_miner(world, entity, try_transform_miner_not_full)     schedule_action(world, entity,       create_miner_action(entity, action.image_store),       ticks + entities.get_rate(entity))    return tiles 

if @ function, def miner_not_full_action, you'll see line: (tiles, found) = miner_to_ore(world, entity, ore). notice inside function, calling method, miner_to_ore (from class i've provided above).

my question is, correct way rewrite line of code function can use method class (even though function outside of class)? thanks!

you can call methods of class outside if either have object of class or can construct one. except, if static method or class method.

in example, want use from

class minernotfullaction:    def __init__(self, entity, image_store):       self.entity = entity       self.image_store = image_store     def miner_to_ore(self, world, ore):        ... 

in

def miner_not_full_action(world, action, ticks):    entity = action.entity    entity_pt = entities.get_position(entity)    ore = find_nearest(world, entity_pt, entities.ore)    (tiles, found) = minernotfullaction.miner_to_ore(world, entity, ore)    ... 

so, question is: have appropriate ection object here?

if (action sounds appropriate candidate), can do

(tiles, found) = action.miner_to_ore(world, ore) 

in order perform task.

see other side: if call method, must have seen self. should instance of class. if don't have that, have create one. otherwise, self make no sense in method.

easy example:

def miner_not_full_action(world, action, ticks):     # 1. call via class. may or may not work, have provide action object first argument.     try1 = minernotfullaction.miner_to_ore(action, world, ore)     # 2. call regularly (as mentioned), equivalent 1.     try2 = action.miner_to_ore(world, ore)      # if don't have such object? create one:     ac = minernotfullaction()     # , use call:     try3 = ac.miner_to_ore(world, ore) 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -