Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81036
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2014-11-19 11:00 -0800 |
| References | (1 earlier) <m4b92h$2dj$1@ger.gmane.org> <mailman.15925.1416184915.18130.python-list@python.org> <SNGdnY6EVddWYvfJnZ2dnUU7-YWdnZ2d@giganews.com> <89161395-a817-4412-956f-28f7581f3269@googlegroups.com> <5badneHU4qn11_HJnZ2dnUU7-eednZ2d@giganews.com> |
| Message-ID | <caa76927-c865-4868-94ff-12ba4ff99bbc@googlegroups.com> (permalink) |
| Subject | Re: How modules work in Python |
| From | sohcahtoa82@gmail.com |
On Tuesday, November 18, 2014 11:44:53 PM UTC-8, Larry Hudson wrote:
> On 11/18/2014 12:59 PM, sohcah...@gmail.com wrote:
> > On Tuesday, November 18, 2014 12:14:15 AM UTC-8, Larry Hudson wrote:
> >> First, I'll repeat everybody else: DON'T TOP POST!!!
> >>
> >> On 11/16/2014 04:41 PM, Abdul Abdul wrote:
> >>> Dave,
> >>>
> >>> Thanks for your nice explanation. For your answer on one of my questions:
> >>>
> >>> *Modules don't have methods. open is an ordinary function in the module.*
> >>>
> >>> Isn't "method" and "function" used interchangeably? In other words, aren't they the same thing?
> >>> Or, Python has some naming conventions here?
> >>>
> >>
> >> You've already received answers to this, but a short example might clarify the difference:
> >>
> >> #------- Code --------
> >> # Define a function
> >> def func1():
> >> print('This is function func1()')
> >>
> >> # Define a class with a method
> >> class Examp:
> >> def func2(self):
> >> print('This is method func2()')
> >>
> >> # Try them out
> >> obj = Examp() # Create an object (an instance of class Examp)
> >> func1() # Call the function
> >> obj.func2() # Call the method through the object
> >> func2() # Try to call the method directly -- Error!
> >> #------- /Code --------
> >>
> >> This code results in the following:
> >>
> >> This is function func1()
> >> This is method func2()
> >> Traceback (most recent call last):
> >> File "fun-meth.py", line 14, in <module>
> >> func2()
> >> NameError: name 'func2' is not defined
> >>
> >> -=- Larry -=-
> >
> > You COULD have something like this though:
> >
> > # --- myModule.py ---
> > def myFunc():
> > print 'myFunc'
> >
> >
> > # --- main.py ---
> > import myModule
> > myModule.myFunc()
> >
> >
> > In this case, myFunc LOOKS like a method when it is called from main.py, but it is still a function.
> >
>
> My purpose was to give a _simple_ example of the difference in the two terms: that a function
> is called directly and a method is called through an object.
>
> Your example may _look_ the same (it uses the same dot syntax), but here it is to resolve a
> namespace -- a module is not an object. So yes, this is still a function and not a method. But
> we're getting rather pedantic here.
>
> -=- Larry -=-
I only started reading this list about a month ago, and from what I've seen, being pedantic is pretty much par for the course.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: How modules work in Python Abdul Abdul <abdul.sw84@gmail.com> - 2014-11-17 01:41 +0100
Re: How modules work in Python Larry Hudson <orgnut@yahoo.com> - 2014-11-18 00:14 -0800
Re: How modules work in Python sohcahtoa82@gmail.com - 2014-11-18 12:59 -0800
Re: How modules work in Python Larry Hudson <orgnut@yahoo.com> - 2014-11-18 23:44 -0800
Re: How modules work in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-19 22:32 +1100
Re: How modules work in Python sohcahtoa82@gmail.com - 2014-11-19 11:00 -0800
Re: How modules work in Python Chris Angelico <rosuav@gmail.com> - 2014-11-20 08:50 +1100
Re: How modules work in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-19 21:32 -0500
Re: How modules work in Python Michael Torrie <torriem@gmail.com> - 2014-11-20 09:53 -0700
csiph-web