-4

I am new to Python so it would be great if someone can find time to answer my query :

Fido = Dog()

I am able to understand

Fido.size = "tall" 
Fido.sleeps() 

But I am not sure what this means as given in the below link :

http://reeborg.ca/docs/oop_py_en/oop.html

Objects can also have other objects that belong to them, each with their own methods or attributes:

Fido.tail.wags()
Fido.tail.type = "bushy";
Fido.left_front_paw.moves()
Fido.head.mouth.teeth.canine.hurts()

Please help

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
ahimsa
  • 1
  • 2

2 Answers2

0

Fido.tail.type is setting the type variable (attribute) inside the class' .tail() method to "bushy".

In classes, there are functions, which are called methods.

class Person:

    def __init__(self, name):
        self.name = name # Setting the name of the 'person'

    def wave(self): # The methods which is called with George.wave()
        print(self.name + ' waves at you.')
George = Person('George')
George.wave()

Will print "George waves at you."

George
  • 65
  • 6
  • Thanks for your reply .Yes I am able to understand the basic object.attribute or object.method() concept .But not really getting along with object.attribute.attribute.attribute . Please help me visualize. – ahimsa Nov 07 '16 at 20:41
0

To start with, everything is an object in Python. This SO question is a good place to start understanding what it means for something to be an object. What this means is that almost everything in Python has attributes and methods. E.g the string

'foo'

is an object of the string class, and so, has methods and attributes that are shared across other strings such as its length.

In the Fido example, 'tail' is an object that belongs to Fido. This object has a 'type' and a method called 'wags'. Hence, we can say that 'wags' is a method of tail, which is an object found in Fido (which is an instance of the Dog class).

Community
  • 1
  • 1
  • Many Thanks for your answer and the link .Lets look at the line - Fido.head.mouth.teeth.canine.hurts() .Here I understand that head is an attribute of Fido , mouth is an attribute of head ,teeth is an attribute of mouth and so on and finally hurts() is a method of canine.(Please correct me if I am wrong).So should I visualize 'head' as a parent class for 'mouth' and 'mouth' as a parent class for 'teeth' and so on ?If not then please help me visualize the flow. – ahimsa Nov 07 '16 at 20:27
  • Parent and child classes are used to denote objects that inherit attributes and properties from each other. Head, mouth, teeth, and canine are probably defined to be attributes that inherit some values from the object they are contained in: it should be fine to visualize them as 'parent classes' for the objects they contain. – Fruitspunchsamurai Nov 07 '16 at 22:04