On 15/08/18 08:32, Rafael Knuth wrote: > I am trying to wrap my head around naming conventions & semantics in Python.
A good question with a none to simple answer. In truth some of it goes back to essentially arbitrary decisions made by Guido vanRossum when he originally designed Python! The other key concept to remember is that methods are functions attached (bound) to an object(class) and that every value is Python is an object. And that includes functions! > Here are my questions: > > - List is a function, and read is a method, is that correct? Yes, although list() can also be though of as an object constructor - it creates instances of list objects > - Semantic wise it would be always like function(something) and a > method would be something.method() .. is that correct? Yes. Methods are accessed via their object. It is also possible (but usually a bad idea) to call methods via their class like functions: aClass.method(theObject, something,...) > - 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? As mentioned above methods are functions. But they are functions attached to objects. Thats what makes them methods. They are defined in the class definition from which the object is instantiated. They have a tiny bit of magic associated with them in that the first parameter of a method is a placeholder for the object it works on - usually called self. When you call a method Python looks up the original class and calls the function defined there and substitutes the current object for self, thus: class MyClass: def method(self): pass myObject = MyClass() # create an object myObject.method() is now converted by Python to: myClass.method(myObject) > - For example, why is print a function and not a method? > something.print() instead of print(something) Because print can print anything, its not restricted to a single class of object. (Although it could have been defined at the "object" level and thus apply to all objects. But thats the arbitrary choice Guido made.) There are other similar functions like len() There are also rather confusingly some commands. They look like functions but don't require parens. The most common is probably del x = 42 del x # not del(x), although that works too. print was a command in Python 2 but got converted to a function in Python 3. For some reason del and a few others did not... > Just trying to understand logic of Python and conventions, which will > then make learning and memorizing things easier. A good thing to do but in some cases you have to settle for "that's just the way Guido made it" :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor