3

I have read that behavior of an object is the action performed by the object(in real life situation) but in programming, context is it not the operation/action performed on the object as described by the method.

For example in the code below we have a method to display the full name of the employee. In this case, the action is performed by the object or is it the action is performed on the object?. How can displaying full name be a behavior of an object when we compare it to the behavior of a dog object(Like barking, sleeping, etc). Thanks for your help

class employee:

    raise_value=1.05
    def __init__(self,first,last,pay):

        self.first=first
        self.last=last
        self.pay=pay

    def emp_fullname(self):
        return "Full name is {} {}".format(self.first,self.last)

    def set_raise(self):
        self.new_sal=float(self.pay)*employee.raise_value
        return 'My new salary is {}'.format(self.new_sal)

e1=employee("Chyanit","Singh","60000")
e2=employee("Parag","Singh","40000")

e1.emp_fullname()
Wondercricket
  • 7,651
  • 2
  • 39
  • 58
  • There's no code here that *displays* a full name. Your `employee` class has a function which returns a string. It's really more of a property, since it doesn't actually *do* anything. – Blorgbeard May 01 '19 at 20:56
  • also note you can mark methods like `emp_fullname` with [`@property`](https://stackoverflow.com/q/17330160/1358308) which might end up closer to what you're expecting – Sam Mason May 01 '19 at 21:07
  • I think language can be tricky here. If the methods belong to the object you can say the actions are being done by the object, whereas if they are external methods or functions you can say they act on the object. Of course in the first case, the object may act on itself too. – progmatico May 01 '19 at 21:15
  • What does it mean that the type of an object determines the operations that an object supports in python can you please clarify with an example – Chyanit Singh May 02 '19 at 05:43

2 Answers2

3

The behaviour of an object is always in response to an external stimulus. In OO, a message is broadcast by "something" and your object responds in some way. If the message is a request for information, it would trigger the knowledge responsibility of the object; if the message is an event or a request to do something, it would trigger a behaviour responsibility. The operations would depend on the state of the object and the nature of the event. Sometimes an event contains a "snapshot" of the current overall state of the system that gives a context for the object operations to execute. Note that whatever the object does, it does not affect its external context.

There is a third responsibility which is to provide a service. Most OOP programming actually caters for services, which are just transforms of the contexts passed to them without any effect on the objects themselves. There is very little behaviour or knowledge responsibility in them.

In your employee class, you have both knowledge and behaviour responsibilities. An instance of the class would be able to respond to a request for the full name of the employee and to "behave" in such a way as to change its internal state, that is, getting a new salary. The only thing I would change is renaming set_raise to raise_salary to make it look less of a setter method.

RWRkeSBZ
  • 723
  • 4
  • 11
  • can you please explain this in simple terms "The operations would depend on the state of the object and the nature of the event. Sometimes an event contains a "snapshot" of the current overall state of the system that gives a context for the object operations to execute. Note that whatever the object does, it does not affect its external context" Thank you so much for your answer – Chyanit Singh May 02 '19 at 15:03
  • The effect of an operation would depend on the state of the object: for example, if you have an upper limit for salary, your operation would not yield any change if that limit is reached when you call `set_raise`. An event would contain a snapshot...: your object knows only about itself, so when a method is invoked as a result of an event, it is passed arguments that represent a snapshot of the context. An object does not affect its external context: the object itself *may* change when its method is invoked, but it cannot change its external context. – RWRkeSBZ May 02 '19 at 16:29
0

Methods are performed by or on an object. It doesn't have to be one or the other. GetFullName() and SetFullName(first,last) are both methods. The former is performed by the object; like a dog barking, it gives you something. The latter is performed on the object like when you name your new dog or give it a bone.

This question may or may not help you understand what a method is: What's the difference between a method and a function?

cpcolella
  • 247
  • 4
  • 10