P. 1
Python 3: Byte of Python

Python 3: Byte of Python

4.83

|Views: 3,311|Likes:
Published by eebeta
a very good book to learn Python as a beginner, from http://www.swaroopch.com/notes/Python
a very good book to learn Python as a beginner, from http://www.swaroopch.com/notes/Python

More info:

Published by: eebeta on Feb 12, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less

04/08/2015

We have already discussed that classes/objects can have methods just like functions except
that we have an extra self variable. We will now see an example.

#!/usr/bin/python
# Filename: method.py

class Person:
def sayHi(self):
print('Hello, how are you?')

p = Person()
p.sayHi()

# This short example can also be written as Person().sayHi()

Output:

Python en:Object Oriented Programming

82

$ python method.py
Hello, how are you?

How It Works:
Here we see the self in action. Notice that the sayHi method takes no parameters but
still has the self in the function definition.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->