An Introduction To Python Objects Using IPython (Part-3) [ID:010] (3/3)
in series: An Introduction to Python Objects, using IPython
video tutorial by Jerol Harrington, added 03/07
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
In this final ShowMeDo, we cover the __init__ method, bound vs unbound methods, passing objects by reference, data attributes, and conclude with some recommendations for further self-development
Got any questions?
Get answers in the ShowMeDo Learners Google Group.
Video statistics:
- Video's rank shown in the most popular listing
- Video plays: 1955 (since July 30th)
- Plays in last week: 0
- Published: Sometime before 1st March 2007 (in other words - we don't remember!)
Thank-yous, questions and comments
If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?
You may also want to see our ShowMeDo Google Group to speak to our active users and authors.
All comments excluding tick-boxed quick-comments
Thanks for the series.
Very well presented
excellent introduction. a bit more of errors and exercises on my own and i am into what classes are. no complex, but tricky...
Thank you for the lesson. This is very good. I was learning while doing the examples by myself. It would be helpful if we can have the following.
1. A link to the scripts used so that the learner can get a full view and copy it to his session.
2. Bullet points of the the key take aways from the lesson at the end of the session.
Very Nice. Thanks!
Jerol,
The three tutorials you made on Python objects were excellent.
You have a knack for explaining things clearly.
Please make more Python tutorials.
Thanks a lot for this nice introduction to the Python way of objects. :-)
Thanks Jerol. Since www is a class which should be put in camel case, it might have been easier to understand if you had named it Clas (just as you named the function func).
Viewed this one twice since there was so much content to learn. Very good content.
Excellent
Excellent
Thanks,
I'm writing a game-like life simulation in python and was really getting stumped about how to create more than one instance of an animal with different positions, etc.
This should really help.
Really, really good. I am starting to understand more about classes. I would still like to see a bit more of the usefullness of this. For example, I am still confused as to why use a class within a class? Sorry for asking such a simple question, but I working on an analysis table that has electrical loads and want to get real good with Python. I have used it as a scripting language, but have stayed away from the Object Oriented stuff. Thank you very much again.
Thanks Jerol, you made some non-obvious attributes of Python classes very explicit in this video.
Hi anon,
If you look at the code again, you will notice that the Base1 class prints "THIS IS BASE1" in all caps, whereas Sub1 prints "This is sub1!" in lower case. Your outputs tells us that Sub1 isn't being initialized at all. If you look at the first init statement you will see that it is _init__ instead of __init__ (only 1 leading underscore instead of 2). If you fix the init, you should see:
THIS IS BASE1: base1
This is sub1: sub1
This shows that Base1's init is being called, but with a value from Sub1, followed by Sub1's init with its own value.
This can be tricky to get right, so it is a good idea to test your code with as simple a model as you can, and use something like caps and lower case to see if your class instances are being properly initialized.
Thank you for your interest. It is very gratifying to learn that someone is actually trying to replicate the examples.
Jerol
Hi Anon - Jerol Harrington will be notified about your new comment. The easiest way to converse might be to join the Google Group:
http://groups.google.com/group/showmedo
and to re-print the message (I'm afraid our comment-code strips out the indentation!).
Jerol is a part of the Group, you could easily talk about it rather than by writing comments here.
Ian.
Is there a bug in # 3 SHowMeDo or is there a bug in iPython?
How can I contact Mr Harrington?
When I run
y=Sub1()
Only "THIS IS BASE1: BASE1" is displayed.
This is very confusing, and I'd like to resolve it
I'm pretty sure I've entered the code the way the author has it in ShowMeDo #3
class Base1(object):
"base class demo"
def __init__(self, str1="BASE1"):
self.show(str1)
def show(self, str1):
print "THIS IS BASE1: %s" % str1
class Sub1(Base1):
"subclass demo"
def _init__(self, str1="sub1", str2="base1"):
#super(Sub1, self).__init__(str2) # bound method
Base1.__init__(self, str2) # unbound method
self.show1(str1)
def show1(self, str1):
print "This is sub1: %s" % str1
Thanks very much for the tutorial. It clarified a lot of things that I was unsure about concerning classes and objects, simply and concisely.
Just wanted to say thanks for putting in the time . It is useful and needed . I love to program in Python but get very little time to do so and find my self having to re-learn every time. explanations like this help when you're not prograsmming every day.
thanks
As a newbie to Python, OOP concepts, and to some extent, even programming (discounting some rudimentary C code written a long time ago), this is by far the best introduction I've seen to Python objects and classes....and I've been scouring the websites a while.
Thank you! and congrats on a great tutorial.
Thanks very much for the Intro to Python Objects and concepts. This is the beauty of the internet for us in this part of the world. Also very helpful is where to go next and suggestions about the books and using help online.
ShowMeDo is a master stroke...waiting to hear more and tell my colleagues about contributing !
Thanks again.
Thank you very much for doing these, Jerol. As I'm trying to dive into GUI programming and OO, these tutorials made many fundamental points much clearer. Kudos.
Hi T5.4
Thanks for your comment. Python looks so simple and basic, that it is easy to forget that Guido designed it to be OO right from the beginning. It took me awhile to understand objects in Python, and I had taken 2 courses in C++! What has worked for me, is writing the simplest possible fragment of code in an editor and then running it in IPython. This way, I can make changes in the code, and see how the interpreter reacts. I have 2 windows open, and <Alt><Tab> between them.
Thanks again for your kind words.
Jerol
Jerol,
Thank you for taking the time to make these three videos. They have reinforced my knowledge of OOP and the "self" convention in Python. When attempting to learn Python, I did not fully understand this convention and you have helped me greatly. This video should be recommended and watched by anyone wanting to learn object-orientation FIRST! I wish this was around a year ago.
To All,
Thanks for your comments. To zan0416, I will try to incorporate your suggestion to combine the editor screen and the IPython screens. It sounds like a good idea. As for suggestion regarding passing Python objects like structs in C, I have thought about doing a ShowMeDo about Python data structures such as lists and dictionaries. C structs are just a bunch of data laid out end-to-end in a block of memory. This is very easy to do in Python: unlike C, you do not have to declare the data type, and then allocate memory for it, or worry about losing the pointer for the allocated memory: just define a dictionary, using strings to take the place of variable names (keys), and assign whatever data you want to the keys. You can then pass the dictionary to a function or object. An object-oriented approach might be to decide what functions you wanted to associate with the the dictionary, and combine both the functions and dictionary in an object. Keep in mind that C is close to the machine, whereas Python is much more abstract. I found that I had to abandon my C mental metaphors in order to understand Python.
Hopes this helps.
Jerol
As a long time C programmer, an idea for another OOP topic I would love to see someone do is the proper way to setup, use, and pass objects in Python that behave the same way as 'structs' do in C. Possible?
Wonderful Jerol! Your examples clarified more than one mystery about objects I have been struggling with.
A suggestion if you find yourself modifying it, or doing it over for any reason, I wonder if it is possible to display both the Crimson Editor and IPython windows at the same time (looks like there is enough room), rather than switching between them. Seeing the actual source code as the examples are run in IPython would be super.
Thanks for the video!
Thank you. A good nuts and bolts tutorial on objects. Cleared up a couple of things that I've been unsure about, while studying Python.
Thanks for the vid.
I learn very quickly by being shown once.
