python terminology

A forum for general discussion of the Python programming language.

python terminology

Postby duxbuz » Sat Jan 11, 2014 8:56 am

hi

I am trying to understand some of the terminology in python in regards to using the Turtle module and its methods such as forward(100) etc.

I think I have it correct in saying that in regards to the command "turtle.forward(100)" (is it a command? more questions) turtle is a module and forward is a method?

Could it also be described that turtle is an object(as everything seems to be in python) and forward is a method?

I will list the options i am unsure of which it could be:

1. turtle is a module forward is a method
2. turtle is a module forward is a function

3. turtle is an object forward is a method
4. turtle is an object forward is a function

5. turtle is a class forward is a method
6. turtle is a class forward is a function


in regards to a quote from http://docs.python-guide.org/en/latest/ ... structure/
"However, unlike Java, Python does not impose object-oriented programming as the main programming paradigm. It is perfectly viable for a Python project to not be object-oriented, i.e. to use no or very few class definitions, class inheritance, or any other mechanisms that are specific to object-oriented programming."


So when describing the turtle.forward syntax, it would then seem better not to use the word "object". As although things are objects the program might not really be being used in an OOP context, if this makes sense. But then would I use the term 'method' if not viewing things in an OOP paradigm?

Or is it the opposite of this and objects and OOP are unequivocally the paradigm of a program using the turtle module?

I hope this is clear. I would be grateful for any help from anyone with any clarity on these questions.

Regards
Last edited by micseydel on Sat Jan 11, 2014 11:27 am, edited 1 time in total.
Reason: Locked OP.
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: python terminology

Postby micseydel » Sat Jan 11, 2014 11:51 am

When you write pure Python code, you can do so with the procedural paradigm instead of OOP. Turtle is an existing module for which someone has already made a decision that the source you were reading from said you have. If you decide to use that module in your source code, you're somewhat forced to do so.

Turtle is a module. When you import it, it's an object in your program. It is not a class; something cannot be a module and a class. When you use the forward() function, it's not a method, but the reasons for this are complicated. A good rule of thumb is that functions associated with a class are methods, and those in a module are functions. (I believe there are complicating examples, but it's still a good rule of thumb.) You can read the turtle source code, since it's on your computer.

People commonly say that "everything is an object in Python". This is very nearly true. return, yield, assert and other reserved words aren't objects, but classes (though not the keyword class), their instances, modules, functions and methods associated with classes are all objects.

Even if you write procedural programs in Python, the functions, dicts, lists, strings, etc. are all objects. And they have methods, even if you choose not to make use of them. If this bothers you, instead of writing
Code: Select all
[1, 2, 3, 4, 5].index(3)

you could write
Code: Select all
list.index([1, 2, 3, 4, 5], 3)

and in this second case, consider list to be a module or namespace rather than an object.

I hope I've been clear here. Feel free to ask anymore questions.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: python terminology

Postby snippsat » Sat Jan 11, 2014 3:04 pm

Some more info as a supplement to micseydel good explanation.
"turtle.forward(100)" (is it a command? more questions) turtle is a module and forward is a method?

If look away from import,let's make the same in a smaller scale.
Code: Select all
class Foo:
    def forward(self, distance):
        return 'Moving {}'.format(distance)

Use this Foo Class.
Code: Select all
>>> turtle = Foo()
>>> turtle.forward(100)
'Moving 100'
>>> turtle.forward(50)
'Moving 50'
 

So here is forward() a method of class Foo,if you look at code in line 1608.
You see that forward() is a method of class TNavigator.
User avatar
snippsat
 
Posts: 267
Joined: Thu Feb 21, 2013 12:04 am

Re: python terminology

Postby micseydel » Sat Jan 11, 2014 9:34 pm

I haven't delved as closely into the Python 3 code as Python 2, but in Python 2
Code: Select all
>>> import turtle
>>> turtle.forward
<function forward at 0xca9f50>
>>>
>>> class Foo:
    def forward(self, distance):
        return 'Moving {}'.format(distance)

>>> Foo.forward
<unbound method Foo.forward>

There was some black magic at the end and I didn't try to decipher it (it involved 4 eval() calls), but just introspecting on the live session indicates it's not directly a class, although I did turn up what you did and there is of course a method for that. When you call turtle.forward() though, you aren't calling a method, you're calling a function.

This is quite nitpicky at this point, but strictly speaking, they are different. I showed above that it's a function rather than a method, although that function probably just wraps the method that snippsat pointed out. See the difference more simply here
Code: Select all
$ cat module.py
class Shape:
    def __init__(self, sides):
        self.sides = sides

    def print_me(self):
        print "I have", self.sides, "sides!"

print_shape = Shape.print_me
wrapped_print = lambda shape: shape.print_me()

$ python
Python 2.7.3 (default, Sep 26 2013, 16:35:25)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import module
>>> rectangle = module.Shape(4)
>>> rectangle.print_me()
I have 4 sides!
>>> module.print_shape
<unbound method Shape.print_me>
>>> module.print_shape(rectangle)
I have 4 sides!
>>> module.wrapped_shape
<function <lambda> at 0x7f622a362320>
>>> module.wrapped_print(rectangle)
I have 4 sides!
Last edited by micseydel on Sat Jan 11, 2014 11:15 pm, edited 1 time in total.
Reason: Had said stranac when it was snippsat, oops! Thanks for the correction.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: python terminology

Postby stranac » Sat Jan 11, 2014 11:02 pm

micseydel wrote:I showed above that it's a function rather than a method, although that function probably just wraps the method that stranac pointed out.

Wasn't me. My posts usually have a beautiful super cute pony next to them, not some pen...

micseydel wrote:There was some black magic at the end and I didn't try to decipher it (it involved 4 eval() calls)

I can't resist black magic, so I did decipher it... I only counted 3 evals per loop, and one exec. :P

It basically just creates the functions you call by using turtle.whatever()
The only thing these do is call the method with the same name of a Turtle object and return the result.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: python terminology

Postby duxbuz » Sun Jan 12, 2014 8:13 am

micseydel wrote:When you write pure Python code, you can do so with the procedural paradigm instead of OOP. Turtle is an existing module for which someone has already made a decision that the source you were reading from said you have. If you decide to use that module in your source code, you're somewhat forced to do so.

Turtle is a module. When you import it, it's an object in your program. It is not a class; something cannot be a module and a class. When you use the forward() function, it's not a method, but the reasons for this are complicated. A good rule of thumb is that functions associated with a class are methods, and those in a module are functions. (I believe there are complicating examples, but it's still a good rule of thumb.) You can read the turtle source code, since it's on your computer.

People commonly say that "everything is an object in Python". This is very nearly true. return, yield, assert and other reserved words aren't objects, but classes (though not the keyword class), their instances, modules, functions and methods associated with classes are all objects.

Even if you write procedural programs in Python, the functions, dicts, lists, strings, etc. are all objects. And they have methods, even if you choose not to make use of them. If this bothers you, instead of writing
Code: Select all
[1, 2, 3, 4, 5].index(3)

you could write
Code: Select all
list.index([1, 2, 3, 4, 5], 3)

and in this second case, consider list to be a module or namespace rather than an object.

I hope I've been clear here. Feel free to ask anymore questions.


What you said is clear thanks.

There are just some things I don't fully understand.

I can see how a class has methods, and that's easy to remember as that's what Java is like.

But it was when you mentioned that the object example of a list being an object and having methods, but it is an object not a class? As the turtle would be an object. You mentioned before that that only classes have methods.

But I do understand it more and I thank you for your help.
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: python terminology

Postby duxbuz » Sun Jan 12, 2014 8:31 am

snippsat wrote:Some more info as a supplement to micseydel good explanation.
"turtle.forward(100)" (is it a command? more questions) turtle is a module and forward is a method?

If look away from import,let's make the same in a smaller scale.
Code: Select all
class Foo:
    def forward(self, distance):
        return 'Moving {}'.format(distance)

Use this Foo Class.
Code: Select all
>>> turtle = Foo()
>>> turtle.forward(100)
'Moving 100'
>>> turtle.forward(50)
'Moving 50'
 

So here is forward() a method of class Foo,if you look at code in line 1608.
You see that forward() is a method of class TNavigator.


Thanks for your help.

I suppose looking at this point and looking at TNavigator class, it is a method of that class.

My main need for clarity is because I am trying to use some basic programming in a teaching/learning capacity and I have been using python turtle. I suppose from what I have learned here I would not feel too bad using a statement such as "the forward method of the turtle module" or "the forward function of the turtle module" or "the forward method of the turtle object we created" or "the forward function of the turtle object we created"

currently I feel more comfortable saying something like "the forward method from the turtle module" or "the forward function from the turtle module"

Now I know that the true terminology is open to debate, I do not feel like I am giving complete misinformation.

Thanks again.
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: python terminology

Postby duxbuz » Sun Jan 12, 2014 8:41 am

http://fossies.org/dox/Python-3.3.3/turtle_8py_source.html

But to add to this it does say at line 126 - _tg_turtle_functions
and 129 - 'fillcolor', 'filling', 'forward'

then as was mentioned further down it states that it is a method of TNavigator class (with the self argument)

So some ambiguity even in the module? well ambiguous for a novice like myself
duxbuz
 
Posts: 9
Joined: Sat Jan 11, 2014 8:37 am

Re: python terminology

Postby snippsat » Sun Jan 12, 2014 10:09 am

But to add to this it does say at line 126 - _tg_turtle_functions
and 129 - 'fillcolor', 'filling', 'forward'

then as was mentioned further down it states that it is a method of TNavigator class (with the self argument)

So some ambiguity even in the module? well ambiguous for a novice like myself

There are some wrapping and magic stuff in this code,so not best code to look at for learning.
Turtle is easy to use,but look at actual code is not so fun because all stuff they have done to get a easy user interface.
User avatar
snippsat
 
Posts: 267
Joined: Thu Feb 21, 2013 12:04 am

Re: python terminology

Postby stranac » Sun Jan 12, 2014 1:14 pm

You still seem to be slightly confused.

duxbuz wrote:But it was when you mentioned that the object example of a list being an object and having methods, but it is an object not a class?

Instances of classes also have methods.

And just to clear out any confusion about turtle.forward():

turtle.TNavigator class has a method named forward(), as do RawTurtle and Turtle which inherit from it.
You are not calling this method when calling turtle.forward().
turtle.forward() is a function from the turtle module, not a method of a class/object.

The function does, however, serve as a proxy for the method...
It creates an anonymous Turtle instance(or just gets it if it already exists), and calls its forward() method.

But as snippsat said, looking at the turtle.py code is probably not something you should do for learning. Way too magicky to be a learning tool...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest