Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2012, 08:45 AM
Lipska the Kat
Guest
 
Posts: n/a
Default Encapsulation, inheritance and polymorphism

Pythoners

Python 2.7.3
Ubuntu Linux 12.04 LTS

I've been taking a brief look at Python.

From the tutorial documentation I get the following

'Python is an easy to learn, powerful programming language. It has
efficient high-level data structures and a simple but effective approach
to object-oriented programming'.

I was expecting (hoping) to see in depth documentation relating to Class
construction, extension mechanisms and runtime polymorphism.

What I actually get is a confusion of Classes, modules, scripts and
whatever else.

Is Python truly OO or is it just one way to use the language. I see some
documentation relating to classes but nothing on instantiation .. in
fact the documentation appears to say that classes are used in a static
way e.g ClassName.method(), and command line scripting is really outside
the scope of other OO languages I have experienced.

Is there a previous discussion in the group that I could read.

Many thanks

Lipska

--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-17-2012, 09:30 AM
Ulrich Eckhardt
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

Welcome!

Am 17.07.2012 10:45, schrieb Lipska the Kat:
> I was expecting (hoping) to see in depth documentation relating to Class
> construction, extension mechanisms and runtime polymorphism.


In addition to this forum for direct help and discussion, two
suggestions: Firstly, it could help if you mentioned what programming
languages you are fluent in, in order to help traditional misconceptions
and to draw parallels. Secondly, http://docs.python.org is the central
hub to tutorials and documentation.


> What I actually get is a confusion of Classes, modules, scripts and
> whatever else.


Due to the very dynamic nature of Python, types (classes), modules and
functions are themselves objects that can be manipulated.


> Is Python truly OO or is it just one way to use the language.


Python supports OOP, but it doesn't enforce it. You can use other
paradigms, too.


> I see some documentation relating to classes but nothing on
> instantiation .. in fact the documentation appears to say that classes
> are used in a static way e.g ClassName.method(), and command line
> scripting is really outside the scope of other OO languages I have
> experienced.


I think you are confused. For the documentation, it would help to know
which documentation exactly seems to make such claims. For the thing
about commandline scripting, I'm afraid you will have to adjust your
expectations.

BTW: In general, you instantiate a class by just calling the class' name
like a function. If e.g. ClassName is a class, ClassName() instantiates
this class.


Good luck!

Uli
Reply With Quote
  #3 (permalink)  
Old 07-17-2012, 10:03 AM
Devin Jeanpierre
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On Tue, Jul 17, 2012 at 4:45 AM, Lipska the Kat <lipska@lipskathekat.com> wrote:
> Is Python truly OO or is it just one way to use the language. I see some
> documentation relating to classes but nothing on instantiation .. in fact
> the documentation appears to say that classes are used in a static way e.g
> ClassName.method(), and command line scripting is really outside the scope
> of other OO languages I have experienced.


It doesn't look like you're reading the section on classes:

http://docs.python.org/tutorial/classes.html

You create a class like this:

class MyClass(MySuperclass1, MySuperclass2):
# class attributes and methods
attr = 3
def method(self): print self.attr

You can instantiate it by calling the class, as if it were a function:

myinstance = MyClass()

You call a method getting the method (myinstance.method), and then
calling that as if it were a function.

myinstance.method()

Polymorphism is "duck typed" -- that is, it's based on the
presence/absence of attributes and methods, not on declared interfaces
(Python has no interfaces). So, for example, "foo.read()" works on any
object bound to the name "foo", as long as it has a read method that
takes no parameters. This could be a file-like object, but it could
also be something completely different that just happens to have a
method by the same name.

> Is there a previous discussion in the group that I could read.


Man, I dunno, the list archives are impossible to navigate.

-- Devin
Reply With Quote
  #4 (permalink)  
Old 07-17-2012, 11:23 AM
Dave Angel
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 07/17/2012 07:01 AM, Lipska the Kat wrote:
> <SNIP>
>
> Anyway, I'm looking at Python as a rapid prototyping language.
> I have an idea and just want to get it down in basic outline code as
> quickly as possible before it departs my aging brain... I'm not used
> to using variables without declaring their type ... (well I used to do
> Visual Basic many years ago) It just seems so weird, and what's this
> obsession with 'correct' indentation of code ???
>


Welcome to comp.lang.python. I hope you enjoy learning and using Python.

Indentation isn't just custom in Python. It's part of the syntax.
Other languages use braces, or keywords, to indicate scope, but Python
uses indentation. Other than the occasional tab to confuse things, the
rules are pretty simple.

You must indent the body of a function, the scope of an if or else
clause, or other similar language pieces (class, try, except, ...)
Within such a scope, you cannot change indentation, except of course for
a nested scope.
At the end of such scope you must outdent to the previous state.

The convention is to use 4 spaces per indentation, but the language will
accept any amount, as long as it's consistent within any single scope.
And although mixing tabs and space worked in Python 2.x, sort of, it's
disallowed in Python 3.

An expression may span multiple lines, but only if it's unambiguous to
the compiler (eg. a pending left paren with no matching right paren
yet). In that case. indentation of the subsequent lines is unrestricted.

HTH

--

DaveA

Reply With Quote
  #5 (permalink)  
Old 07-17-2012, 11:24 AM
Lipska the Kat
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/12 11:03, Devin Jeanpierre wrote:
> On Tue, Jul 17, 2012 at 4:45 AM, Lipska the Kat<lipska@lipskathekat.com> wrote:
>> Is Python truly OO or is it just one way to use the language. I see some
>> documentation relating to classes but nothing on instantiation .. in fact
>> the documentation appears to say that classes are used in a static way e.g
>> ClassName.method(), and command line scripting is really outside the scope
>> of other OO languages I have experienced.

>
> It doesn't look like you're reading the section on classes:
>
> http://docs.python.org/tutorial/classes.html


Thanks, I wasn't, there is too much to do and not enough time to do it.
So now I just need to chill out a bit and get into 'documentation
reading mode' again


> Polymorphism is "duck typed" --


Yes, I've been reading the wikipedia entry on this ... this type of
polymorphism is not familiar to me ... I come from a Java background.
more reading I guess.

snip

> -- Devin


Thanks for your time and I'll try to do a bit better with the reading
thing before asking more questions... not sure about this obsession with
code indentation though :-|


--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer of Aeryn Sun.
Reply With Quote
  #6 (permalink)  
Old 07-17-2012, 11:37 AM
Andrew Berg
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 7/17/2012 6:01 AM, Lipska the Kat wrote:
> Anyway, I'm looking at Python as a rapid prototyping language.
> I have an idea and just want to get it down in basic outline code as
> quickly as possible before it departs my aging brain... I'm not used to
> using variables without declaring their type ... (well I used to do
> Visual Basic many years ago) It just seems so weird, and what's this
> obsession with 'correct' indentation of code ???

"Pythonic" is (or at least should be) a word you encounter frequently in
discussions of Python code. Learn what is considered Pythonic and then
write Python code that way if you want to work with the language rather
than fight it. Duck-typing is very Pythonic and so is readable code. As
Dave mentioned, indentation is part of the syntax - blocks must be
indented with either tabs or spaces (choose one - if you mix them
ambiguously, an IndentationError will be raised). Try "from __future__
import braces" and "import this" for some insight.

The official tutorial gives a great overview of the language and has
links to reference material that goes into greater detail:
http://docs.python.org/tutorial/ (Python 2.7)
http://docs.python.org/py3k/tutorial/ (Python 3.2)

On a side note, I would highly recommend learning Python 3 (3.2 is the
latest stable version) unless you have an explicit need for Python 2
(some major 3rd-party libraries have not been ported yet). Python 2
won't get any new features; it will simply get bug fixes until its EOL
in 2014 (15?).
--
CPython 3.3.0b1 | Windows NT 6.1.7601.17803
Reply With Quote
  #7 (permalink)  
Old 07-17-2012, 11:44 AM
Lipska the Kat
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/12 12:37, Andrew Berg wrote:
> On 7/17/2012 6:01 AM, Lipska the Kat wrote:
>> Anyway, I'm looking at Python as a rapid prototyping language.


snip

> "Pythonic" is (or at least should be) a word you encounter frequently in
> discussions of Python code. Learn what is considered Pythonic and then
> write Python code that way if you want to work with the language rather
> than fight it. Duck-typing is very Pythonic


You're not kidding, the 'duck' example at
http://en.wikipedia.org/wiki/Duck_typing made me check I hadn't overdone
the medication this morning. That is just plain ...weird. It will take
me a while to form non knee jerk opinions of this for sure.

snip

> On a side note, I would highly recommend learning Python 3 (3.2 is the
> latest stable version) unless you have an explicit need for Python 2
> (some major 3rd-party libraries have not been ported yet). Python 2
> won't get any new features; it will simply get bug fixes until its EOL
> in 2014 (15?).


I'll check it out, thanks.

Lipska


--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer of Aeryn Sun.
Reply With Quote
  #8 (permalink)  
Old 07-17-2012, 11:53 AM
Andrew Berg
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 7/17/2012 6:44 AM, Lipska the Kat wrote:
> I'll check it out, thanks.

I forgot to add this:
http://wiki.python.org/moin/Python2orPython3

It's a little outdated (there is more progress toward py3k by 3rd-party
libraries every day), but still quite helpful.
--
CPython 3.3.0b1 | Windows NT 6.1.7601.17803
Reply With Quote
  #9 (permalink)  
Old 07-17-2012, 12:01 PM
Lipska the Kat
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/12 09:45, Lipska the Kat wrote:
> Pythoners
>
> Python 2.7.3
> Ubuntu Linux 12.04 LTS
>
> I've been taking a brief look at Python.
>


snip

Well I've set myself a task.
I have a text file containing a list of stock items
each line contains the number in stock followed by a tab followed by the
name of the item. I need to implement something that reads in the text
file and outputs the stock list in ascending or descending order of
quantity.

Please note I am NOT asking for solutions.

In bash this is laughably trivial

sort -nr $1 | head -${2:-10}

Java is easy but long winded and takes a while to set up

C is ... well it's been a while but I'll get there

Python, well that's my current task. It will be interesting to compare
the solutions for speed and ease of development and more importantly
re-usability.

>
> Lipska
>



--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer of Aeryn Sun.
Reply With Quote
  #10 (permalink)  
Old 07-17-2012, 12:18 PM
Ulrich Eckhardt
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

Am 17.07.2012 13:01, schrieb Lipska the Kat:
> On 17/07/12 10:30, Ulrich Eckhardt wrote:
>> Am 17.07.2012 10:45, schrieb Lipska the Kat:
>>> I was expecting (hoping) to see in depth documentation relating to Class
>>> construction, extension mechanisms and runtime polymorphism.

>>
>> In addition to this forum for direct help and discussion, two
>> suggestions: Firstly, it could help if you mentioned what programming
>> languages you are fluent in

>
> For the past 9 years I have been developing in Java [...]


Java is usually called an OOP language, because everything you do there
is put into a class. Free functions don't exist, the closest you get is
class-static functions (correct me if I'm wrong, I'm not really fluent
in that language). In Python, you have the choice to use OOP, but you
can also use free functions or mix those.


> I'm not used to using variables without declaring their type


As a C++ programmer (roughly 80%C++, 15%Python, 5%C) I know that
feeling. Having types declared in advance just helps by having the
compiler check if the passed arguments are correct. Not having this
gives both freedom but also bears dangers.


> what's this obsession with 'correct' indentation of code ???


You'll get used to it and then start loving it.



Uli

Reply With Quote
  #11 (permalink)  
Old 07-17-2012, 01:29 PM
Mark Lawrence
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/2012 12:01, Lipska the Kat wrote:
>
> Anyway, I'm looking at Python as a rapid prototyping language.
>
> Lipska
>


One of the huge advantages of Python here is that you can simply blast
stuff into the interactive prompt and see what happens, no need to write
a script.

--
Cheers.

Mark Lawrence.



Reply With Quote
  #12 (permalink)  
Old 07-17-2012, 01:35 PM
Mark Lawrence
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/2012 12:44, Lipska the Kat wrote:
>
> You're not kidding, the 'duck' example at
> http://en.wikipedia.org/wiki/Duck_typing made me check I hadn't overdone
> the medication this morning. That is just plain ...weird. It will take
> me a while to form non knee jerk opinions of this for sure.
>
> Lipska
>
>


It's difficult to get junkies off of their addictive substances but I'm
sure that the qualities of Python will eventually overcome your Java
habit

--
Cheers.

Mark Lawrence.



Reply With Quote
  #13 (permalink)  
Old 07-17-2012, 01:52 PM
Roy Smith
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

In article <-8SdnVrXGqie25jNnZ2dnUVZ7qKdnZ2d@bt.com>,
Lipska the Kat <lipska@lipskathekat.com> wrote:

> I'm not used to using variables without declaring their type


If you truly wanted to recreate this type-bondage style of programming
in Python, it's easy enough to do.

Where you would write in C++:

// Type matching will get checked at compile-time
void my_function(MassivelyParallelFrobinator& mpf, OtherThing& ot) {
blah, blah, blah
}

you could write in Python:

# Type matching will get checked at run-time
def my_function(mpf, ot):
assert isinstance(mpf, MassivelyParallelFrobinator)
assert isinstance(ot, OtherThing)

but that's just not the way people write code in Python.
Reply With Quote
  #14 (permalink)  
Old 07-17-2012, 02:01 PM
Lipska the Kat
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 17/07/12 12:37, Andrew Berg wrote:
> On 7/17/2012 6:01 AM, Lipska the Kat wrote:


snip

> On a side note, I would highly recommend learning Python 3 (3.2 is the
> latest stable version) unless you have an explicit need for Python 2
> (some major 3rd-party libraries have not been ported yet). Python 2
> won't get any new features; it will simply get bug fixes until its EOL
> in 2014 (15?).


Wow, that was a blast from the past
Just downloaded, unzipped, untarred, configured, made and installed
python 3.2.3 ... it's YEARS since I've done this, makes me feel young again.

Lispka


--
Lipska the Kat: Troll hunter, Sandbox destroyer
and Farscape dreamer of Aeryn Sun.
Reply With Quote
  #15 (permalink)  
Old 07-17-2012, 02:16 PM
Andrew Berg
Guest
 
Posts: n/a
Default Re: Encapsulation, inheritance and polymorphism

On 7/17/2012 9:01 AM, Lipska the Kat wrote:
> Wow, that was a blast from the past
> Just downloaded, unzipped, untarred, configured, made and installed
> python 3.2.3 ... it's YEARS since I've done this, makes me feel young again.

Most Linux distributions should have a premade package for stable Python
versions - apt-get install python3 or something like that. Python 2 is
generally the default Python because of all the system scripts that
haven't been ported to py3k.
--
CPython 3.3.0b1 | Windows NT 6.1.7601.17803
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:09 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.