Quantcast

OOPs Concept

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

OOPs Concept

Kaushal Shriyan-2
Hi All

I wanted to understand about OOPs Concept in Python in a easy way,
Please explain me with an example

I have been reading http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
but at the moment still the concept is not clear

Thanks in Advance

Regards

Kaushal
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: OOPs Concept

Matthew White-3
Even though I am still new to python, I've recently had an insight as
to what makes OOP different from procedural programming.

Let's take perl for example.  A variable in perl is like a bowl.  It's an
empty vessel you can put things in.  You can change the contents of
the bowl, you can empty the bowl but it doesn't really *do* anything.
It has no real attributes aside from the fact that it's a container.

So when I create a variable in perl it looks like this:

$x = 'hello'

If I want to make the first letter of the value of $x a capital letter,
I have to use a function to do it:

$y = ucfirst($x)

now $y contains the value 'Hello'

In python one doesn't really create variables, one creates objects.
Sring objects, list objects, etc.  And objects are cool because they can
do things.  They are more than just merely bowls, they are like swiss
army knives.  So in python, if I say:

x = 'hello'

Then I can do all sorts of things with x:

x.capitalize()  -> 'Hello'
x.swapcase() -> 'HELLO'
x.count('l') -> 2

This is just a very small example but I hope that my example can help
you understand what objects are what makes OOP different from procedural
programming.

-mtw

On Wed, Apr 19, 2006 at 06:07:27PM +0530, Kaushal Shriyan ([hidden email]) wrote:

> Hi All
>
> I wanted to understand about OOPs Concept in Python in a easy way,
> Please explain me with an example
>
> I have been reading http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
> but at the moment still the concept is not clear
>
> Thanks in Advance
>
> Regards
>
> Kaushal
> _______________________________________________
> Tutor maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/tutor

--
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: OOPs Concept

Kaushal Shriyan-2
On 4/19/06, Matthew White <[hidden email]> wrote:

> Even though I am still new to python, I've recently had an insight as
> to what makes OOP different from procedural programming.
>
> Let's take perl for example.  A variable in perl is like a bowl.  It's an
> empty vessel you can put things in.  You can change the contents of
> the bowl, you can empty the bowl but it doesn't really *do* anything.
> It has no real attributes aside from the fact that it's a container.
>
> So when I create a variable in perl it looks like this:
>
> $x = 'hello'
>
> If I want to make the first letter of the value of $x a capital letter,
> I have to use a function to do it:
>
> $y = ucfirst($x)
>
> now $y contains the value 'Hello'
>
> In python one doesn't really create variables, one creates objects.
> Sring objects, list objects, etc.  And objects are cool because they can
> do things.  They are more than just merely bowls, they are like swiss
> army knives.  So in python, if I say:
>
> x = 'hello'
>
> Then I can do all sorts of things with x:
>
> x.capitalize()  -> 'Hello'
> x.swapcase() -> 'HELLO'
> x.count('l') -> 2
>
> This is just a very small example but I hope that my example can help
> you understand what objects are what makes OOP different from procedural
> programming.
>
> -mtw
>
> On Wed, Apr 19, 2006 at 06:07:27PM +0530, Kaushal Shriyan ([hidden email]) wrote:
> > Hi All
> >
> > I wanted to understand about OOPs Concept in Python in a easy way,
> > Please explain me with an example
> >
> > I have been reading http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
> > but at the moment still the concept is not clear
> >
> > Thanks in Advance
> >
> > Regards
> >
> > Kaushal
> > _______________________________________________
> > Tutor maillist  -  [hidden email]
> > http://mail.python.org/mailman/listinfo/tutor
>
> --
> Matthew White - District Systems Administrator
> Tigard/Tualatin School District
> 503.431.4128
>
> "The greatest thing in this world is not so much where we are, but in
> what direction we are moving."   -Oliver Wendell Holmes
>
>

Thanks Matthew
Just wanted to know
x.count('l') -> 2 Here 2 means what I didnot understood this
and also does x is a object and capitalize(), swapcase()  and
count('l') are methods, is that correct what i understand

Awaiting your earnest reply

Regards

Kaushal
_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: OOPs Concept

Matthew White-3
On Wed, Apr 19, 2006 at 09:10:41PM +0530, Kaushal Shriyan ([hidden email]) wrote:
> Thanks Matthew
> Just wanted to know
> x.count('l') -> 2 Here 2 means what I didnot understood this
> and also does x is a object and capitalize(), swapcase()  and
> count('l') are methods, is that correct what i understand

yes, count(), capitalize(), and swapcase() are called methods.

The count() method counts the number of the given string in the object.

x = 'hello'
x.count('l')     -> 2
x.count('h')     -> 1

y = 'good morning'
y.count('o')     -> 3
y.count('oo')    -> 1

you can find out which methods belong to an object using the dir()
function and can get help for a method by using the help() function:

% python
Python 2.3.5 (#2, Sep  4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'hello'
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode',
'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha',
'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
>>> help(x.swapcase)
Help on built-in function swapcase:

swapcase(...)
        S.swapcase() -> string
           
    Return a copy of the string S with uppercase characters
    converted to lowercase and vice versa.
>>>


-mtw

--
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes

_______________________________________________
Tutor maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/tutor
Loading...