functions vs methods

Sharan Basappa sharan.basappa at gmail.com
Sat Jul 21 23:41:10 EDT 2018


Thanks a lot.

On Sunday, 22 July 2018 04:02:23 UTC+5:30, Rick Johnson  wrote:
> On Saturday, July 21, 2018 at 2:06:21 PM UTC-5, Sharan Basappa wrote:
> > Is there a difference between functions and methods in Python.
> 
> Generally speaking, functions and methods are basically two
> words describing the exact same thing: "small packages of
> reusable code". 
> 
> An easy way to think about it for the
> uninitiated is that, functions came first, and methods are a
> special kind of function which is owned by what we
> programmers call "Objects". 
> 
> You can learn more about methods
> and objects (and a wholelotta other stuff too) by reading
> up on "Object Oriented Programming" (aka: OOP). There are
> tons of tutorials and examples on the web. Ask Google. 
> 
> "What???" o_O
> 
> "Can you repeat that?..."
> 
> [listening carefully]
> 
> "Her???"
> 
> "No, not _her_!"
> 
> "To your left a bit..."
> 
> "..."
> 
> "No, not _that_ left..." (/ロ°)/
> 
> "Your _other_ left!" \(°ロ\)
> 
> "..."
> 
> "Oh... for guido's sake, man..." (-‸ლ)
> 
> "THE ONE WITH THE _PURPLE_ HAIR!!!" (屮゚Д゚)屮
> 
> "..."
> 
> "BINGO!!!" :-)
> 
> "Now, ask her..." 
> 
> 
> > [...]
> > 
> > A related question.
> > 
> > Refer to the following lines:
> > 1) len(list) 
> > 2) list.append()
> > 
> > The first line passes list to a len function while the
> > second line calls append method in list.  So, how to
> > interpret this?  In the first case, len is a function that
> > python provides to which list can be passed and in the
> > second case, append is a method within list class?
> 
> 
> First, get yer terminology correct...
> 
> list _object_. 
> 
> Not _class_. 
> 
> _Object_!!!
> 
> "Class" refers to the syntactical representation of an OOP
> _object_ -- aka: meaning that "class" is all the code
> written to _define_ an object -- while _object_, refers to
> the "live thing" that actually does stuff. For instance: in
> the case of a python list _object_ (1) the _object_ acts as
> a container for other "live objects", and (2) the _object_
> exposes an interface so we can call methods that perform
> certain predefined tasks.
> 
> > If my interpretation is correct, why not make len also as a
> > part of list class itself?
> 
> Simple Answer: because Python is not a _pure_ OOP language. 
> 
> Yes, python allows us to write code in the OOP paradigm (and
> many others), but unlike languages such as, say, Ruby and
> Java (which are pure OOP languages), python decided to go
> down a different route. And there are good reasons to _not_
> be a pure OOP language, and of course, there are certain
> "unintuitive seeming effects" because of that design choice.
> And you've stumbled upon one here. 
> 
> Congrats!
> 
> Once you familiarize yourself with Python's multi-paradigm
> design, and you study more about OOP and experience the
> onerous of purely OOP languages, you will understand why
> python's designers decided to go this route.
> 
> Until then, i'd worry about something more important. Like
> actually mastering the language. And once you've mastered
> the language, then you earn the right to sit around here all
> day and complain about everything. Like any good Pythinistas
> would do.



More information about the Python-list mailing list