Powered By Blogger

Mar 22, 2013


Functions in Python



Functions of Python performs specific task. By using functions we can reduce the duplication of code and can also increase the clarity of the code.  Functions can be assigned into variables, stored in collections or can pass as arguments.

Built-in functions and user defined functions are the two basic types.  Some of the examples of built-in functions are dir(), len() and abs().  The user defined functions are functions created with the ‘def ’  keyword.

The ‘def ’ keyword is followed by the function name with round brackets and a colon.  You can include the indented statements inside the body of the function.

def function_one():
    return ("hello")

When we call the function as function_one()the statements inside the function’s body are executed and gives the output as ‘hello’.

Functions can be defined inside a class those functions are known as ‘methods’.  A function can be defined inside the module (the file which we put the Python code) also the function can be defined inside another function and it’s called an “inner function”.

Methods:
class First_Class:
        @staticmethod
        def f():
        print “f() is a method”

Functions inside the Module:
def f():
        print “f() is a function”

Inner Functions:
def g():
        def f():
        print “f() is an inner function”
        f()


           

Mar 1, 2013


Behaviours 


When concerning the job done by agents it is basically carried out within “behaviours”.  Behaviours are created by extending the jade.core.behaviours.Behaviour class. If you want to make the agent execute a task then you should create an instance of the corresponding Behaviour subclass and call the addBehaviour() method of the Agent class. Each Behaviour subclass must implement following methods.
  •   public void action():  what the behaviour actually does
  •    public boolean done(): Whether the behaviour is finished or not

 There are some Behaviour types as listed below.

  •  “One shot” behaviours.

The class is jade.core.behaviours.OneShotBehaviour . This completes immediately and the action() method is executed only once. Their done() method simply returns true.

  • “Cyclic” behaviours.

The class is jade.core.behaviours.CyclicBehaviour. This never completes and their action() method executes the same operation each time it is invoked. Their done() method simply returns false.

  • “Complex” behaviours.

This completes when a given condition is met.  Embed a state and execute in their action() method, different operation depending on their state.