[Tutor] understanding Python naming conventions & semantics

Ben Finney ben+python at benfinney.id.au
Wed Aug 15 04:30:13 EDT 2018


Rafael Knuth <rafael.knuth at gmail.com> writes:

> - List is a function, and read is a method, is that correct?

* No, ‘list’ is a type. Types are callable; they return an instance of
  the type.

* Yes, a file object has a ‘read’ method.

* Every method is a function. Not every function is a method.

> - Semantic wise it would be always like function(something) and a
> method would be something.method() .. is that correct?

You have shown the syntax for referring to an object and calling it.

The reference syntax will not help you know whether an object is a
method. The calling syntax will not help you know whether an object is a
method.

> - Assuming the above is correct, it seems that there is a tiny line
> between methods and functions? Why is something a method or a
> function?

* Every method is a function.

* A function is a method if it is defined in a class.

> - For example, why is print a function and not a method?

Because the ‘print’ function is not defined in a class.

> Just trying to understand logic of Python and conventions, which will
> then make learning and memorizing things easier.

You may want to refer to the documentation frequently. The Glossary
<URL:https://docs.python.org/3/glossary.html> can help with questions
like this.

-- 
 \      “Not using Microsoft products is like being a non-smoker 40 or |
  `\     50 years ago: You can choose not to smoke, yourself, but it's |
_o__)               hard to avoid second-hand smoke.” —Michael Tiemann |
Ben Finney



More information about the Tutor mailing list