473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to learn OO of python?

I have learned python for over a month.
I heard that it was very easy to learn, but when I tried to know OO of python,
I found it really weird, some expressions seem very hard to understand,
and I can't find enough doc to know any more about it.
So, I wonder how did you master python? And where to find some cool docs?

Thanks.
Jul 19 '05 #1
12 1507
The best one I found was Dive Into Python - and it's free

http://www.diveintopython.org/

Also, How to Think Like a computer scientist - can't remember the link.

Bloke

Jul 19 '05 #2
Here's the link for download:

http://ibiblio.org/obp/thinkCS/python.php

Bloke wrote:
The best one I found was Dive Into Python - and it's free

http://www.diveintopython.org/

Also, How to Think Like a computer scientist - can't remember the link.

Bloke

Jul 19 '05 #3
could ildg <co*******@gmai l.com> writes:
I have learned python for over a month.
I heard that it was very easy to learn, but when I tried to know OO of python,
I found it really weird, some expressions seem very hard to understand,
and I can't find enough doc to know any more about it.


Have you seen the python docs? Maybe this link is of some help.
http://www.python.org/doc/2.4.1/tut/node11.html

Asbjørn
--
Asbjørn Sæbø, post.doc.
Centre for Quantifiable Quality of Service in Communication Systems
Norwegian University of Science and Technology
<URL: http://www.q2s.ntnu.no/ >
Jul 19 '05 #4
could ildg <co*******@gmai l.com> writes:
I have learned python for over a month.
I heard that it was very easy to learn, but when I tried to know OO
of python,
I found it really weird, some expressions seem very hard to understand,
and I can't find enough doc to know any more about it.
These may be hard to understand because you haven't understood OOP.
Python syntax is very similar to p-code used in computer science
books. Try to think like a computer scientist :>
So, I wonder how did you master python? And where to find some cool
docs?


Go to the nearest library and get some OO design book.

--
http://www.peter.dembinski.prv.pl
Jul 19 '05 #5
I think I know what you mean. When I first started trying to learn the
OOP aspect of Python I thought it was strange since I had started with
OOP on Java and C++. Nonetheless, once you get the hang of it, OOP will
make way more sense than all of the complications of Java and C++ class
implementations . Let me give you a quick example of what I'm talking
about:

class Animal:
def eats(self):
print 'The animal eats.'
def walks(self):
print 'The animal walks.'

"""Here the class Dog instantiates the class Animal. 'Dog' will 'do'
whatever 'Animal' does plus some other things which we will describe in
this class code."""
class Dog(Animal):
"""#the self keyword means that this function will be a class
function"""
def communicate(sel f):
print 'The dog barks.'

"""# __init__ function defines what # #will happen as soon as this
class is instantiated. Also, the overloaded variable designators (color
= None, fur = None) allow the instantiator to optionally define these
variables when called from elsewhere."""

def __init__(self, color=None, fur=None):
"""# this time self will designate the variable 'color' # as a
class variable."""
self.color = color
self.fur = fur

if __name__ == '__main__':
sparky = Dog(color="Whit e", fur="Short")
sparky.communic ate()
print sparky.color
print sparky.fur
print sparky.eats() # Here sparky will access 'Animal' methods
that 'Dog' inherited.
print sparky.walks() # Another method originating from the 'Animal'
class.

What happens if you don't use 'self' inside your class code? You won't
be able to access it within the class outside of the method where you
initialized it. For instance, if you don't designate color with self
then you can only use the 'color' variable within the __init__()
method. This is useful whenever you want to use 'color' as a private
variable.

The same also goes for methods. Hopefully, you see how useful this can
be down the road. This way you won't have to use variable/method access
modifiers like Public, Private, Protected and so forth. If this doesn't
make sense or you want more clarity, please let me know.

Good luck,

Harlin Seritt

Jul 19 '05 #6
You might want to look at this first:

http://pigseye.kennesaw.edu/~dbraun/.../UML_tutorial/
http://uml.tutorials.trireme.com/


could ildg wrote:
I have learned python for over a month.
I heard that it was very easy to learn, but when I tried to know OO of
python, I found it really weird, some expressions seem very hard to
understand, and I can't find enough doc to know any more about it.
So, I wonder how did you master python? And where to find some cool docs?

Thanks.


Jul 19 '05 #7
PS: if you're under linux, try umbrello: you design your classes with a
graphical tool and umbrello will generate the code (Python too) for you.
Philippe C. Martin wrote:
You might want to look at this first:

http://pigseye.kennesaw.edu/~dbraun/.../UML_tutorial/
http://uml.tutorials.trireme.com/


could ildg wrote:
I have learned python for over a month.
I heard that it was very easy to learn, but when I tried to know OO of
python, I found it really weird, some expressions seem very hard to
understand, and I can't find enough doc to know any more about it.
So, I wonder how did you master python? And where to find some cool docs?

Thanks.


Jul 19 '05 #8
could ildg wrote:
I think decorator is a function which return a function, is this right?
e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1.

def accepts(*types) :
def check_accepts(f ):
assert len(types) == f.func_code.co_ argcount
def new_f(*args, **kwds):
for (a, t) in zip(args, types):
assert isinstance(a, t), \
"arg %r does not match %s" % (a,t)
return f(*args, **kwds)
new_f.func_name = f.func_name
return new_f
return check_accepts

After I saw all the examples, I concluded that every decorator must
define an inner function which takes only one argument, the
function to decorate. Is this right?


It's close, but not quite right. (I will use the word "function" for
the moment, but see below for why this is inaccurate.) Every
*decorator* must take only one argument, the function to decorate. It is
not at all necessary that a decorator define an inner function.
Consider (from [1]):

def onexit(f):
import atexit
atexit.register (f)
return f

onexit is a decorator because it takes a function and returns a
function. In this case, it happens to be that the same function is
accepted and returned.

Note that in the 'accepts' example above, *check_accepts* is the
decorator, not accepts. The accepts function is actually a function
that *returns* decorators.

Now about that word "function". Decorators are actually *callables*
that accept a single *callable* and return a *callable*. Why does the
terminology matter? Because I can construct decorators from classes too
(from [2]):

class memoized(object ):
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = value = self.func(*args )
return value
except TypeError:
return self.func(*args )

Now the memoized decorator can be used just like any other decorator, e.g.:

@memoized
def fibonacci(n):
if n in (0, 1):
return n
return fibonacci(n-1) + fibonacci(n-2)

Note however that memoized is a *callable*, not a *function*.

STeVe

[1] http://www.python.org/peps/pep-0318.html
[2] http://wiki.python.org/moin/PythonDecoratorLibrary
Jul 19 '05 #9
Steven Bethard:
Thank you so much!
Your answer is very very helpful~

On 5/19/05, Steven Bethard <st************ @gmail.com> wrote:
could ildg wrote:
I think decorator is a function which return a function, is this right?
e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1.

def accepts(*types) :
def check_accepts(f ):
assert len(types) == f.func_code.co_ argcount
def new_f(*args, **kwds):
for (a, t) in zip(args, types):
assert isinstance(a, t), \
"arg %r does not match %s" % (a,t)
return f(*args, **kwds)
new_f.func_name = f.func_name
return new_f
return check_accepts

After I saw all the examples, I concluded that every decorator must
define an inner function which takes only one argument, the
function to decorate. Is this right?


It's close, but not quite right. (I will use the word "function" for
the moment, but see below for why this is inaccurate.) Every
*decorator* must take only one argument, the function to decorate. It is
not at all necessary that a decorator define an inner function.
Consider (from [1]):

def onexit(f):
import atexit
atexit.register (f)
return f

onexit is a decorator because it takes a function and returns a
function. In this case, it happens to be that the same function is
accepted and returned.

Note that in the 'accepts' example above, *check_accepts* is the
decorator, not accepts. The accepts function is actually a function
that *returns* decorators.

Now about that word "function". Decorators are actually *callables*
that accept a single *callable* and return a *callable*. Why does the
terminology matter? Because I can construct decorators from classes too
(from [2]):

class memoized(object ):
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = value = self.func(*args )
return value
except TypeError:
return self.func(*args )

Now the memoized decorator can be used just like any other decorator, e.g..:

@memoized
def fibonacci(n):
if n in (0, 1):
return n
return fibonacci(n-1) + fibonacci(n-2)

Note however that memoized is a *callable*, not a *function*.

STeVe

[1] http://www.python.org/peps/pep-0318.html
[2] http://wiki.python.org/moin/PythonDecoratorLibrary
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
3694
by: Bicho Verde | last post by:
I have now free time and money to do what I want :-) I have some basic skills in programming (C, Pascal, Macromedia Actionscript) but don't know exactly what to do in the world of programming. And also I don't know exactly why would I learn Python rather than C#, C++ or Perl. Basicaly I don't know where to start, if there is much to do or if it is has it seems and there is software to everything nowadays and so doesn't make sense to spend...
24
2523
by: Charif Lakchiri | last post by:
Okay, here's what I know so far about Python: It's an object-oriented scripting language, supported on many platforms. Now here are my questions: It is easy to learn? Does it support GUI programming? Does it support server-side programming, say for web apps? Does it have extensions and libraries, say for DB connectivity, serial com or network programming...? Can it be used for administrative tasks, say as perl...?
14
1721
by: Sam | last post by:
Hi, I have been developing sites and cms's for the past few years using PHP and mysql. I've been interested in learning a new language and was considering Python. I have a pretty decent grasp of OOP concepts (i know, you're skeptical since I mentioned PHP). I don't have a formal programming background, just learning as I go. So, with that being said, here are some dumb questions. 1. What can I do with Python that I can't do with php?
8
1786
by: Aziz McTang | last post by:
Hi Group, I am not an experienced programmer at all. I've learned html and css well enough to hand-write simple websites. I'm now looking to move to the next step. Initially, I'd like to do 3 things: 1) Generate web pages This one's fairly obvious maybe. 2) Create a simplified translation package specific to my line of work:
7
2108
by: fyleow | last post by:
Hi guys, I'm a student/hobbyist programmer interested in creating a web project. It's nothing too complicated, I would like to get data from an RSS feed and store that into a database. I want my website to get the information from the database and display parts of it depending on the criteria I set. I just finished an OO programming class in Java and I thought it would be a good idea to do this in C# since ASP.NET makes web...
5
2003
by: Brian Blais | last post by:
Hello, I was wondering what the approximate minimum age to learn python is. Has anyone had experience teaching middle school students, or elementary school students Python? What brought this up for me is thinking about starting a Lego robots group in a local middle school. I only teach college, and have little experience with middle school students, so I find it hard to guess what they could actually do. I started programming when I...
9
2664
by: Katie Tam | last post by:
I am new to this filed and begin to learn this langague. Can you tell me the good books to start with ? Katie Tam Network administrator http://www.linkwaves.com/main.asp http://www.linkwaves.com
18
1929
by: Amol | last post by:
Hi, I want to learn Python in less than a month which resources should I use. I prefer to read books . Please give me a list of *recognized* resources. Thank You all
65
5247
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
28
1989
by: windandwaves | last post by:
Can someone tell me why I should learn python? I am a webdeveloper, but I often see Python mentioned and I am curious to find out what I am missing out on. Thank you Nicolaas
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.