Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (4)

Powered by Vanilla. Made with Bootstrap.
Simple OOP example
  • chroniccommand
    Posts: 1,389
    Well I've recently been learning about OOP(Object Oriented Programming) in Python. So I've writen up a very simple script that uses OOP.


    #!/usr/bin/env python
    import os, sys

    class DoLs(object): #Here is our first class. They are generally LikeThis.
    def __init__(self): #The initializer. We pass self
    self.ls = os.system('ls')
    print self.ls

    class Dictionary(object): #All of this should look familiar
    def __init__(self, key, value):
    self.dict = {key:value}
    def PrintDict(self):
    print self.dict

    class PrintStuff(object):
    def __init__(self, argument):
    self.arg = argument
    def PrintArgument(self):
    print \"The argument was: \", self.arg
    argz = sys.argv[1]
    printinstance = PrintStuff(argz) #Now to actually use the class
    printinstance.PrintArgument()
    printls = DoLs()
    print(\"\n\n\")
    initdict = Dictionary(argz, \"BoFz\")
    initdict.PrintDict()

    Yes it's simple but its for demonstrating OOP :P

    For anybody learning OOP in Python, I highly recommend the following reads:
    http://www.voidspace.org.uk/python/articles/OOP.shtml
    http://linuxgazette.net/issue56/orr.html
  • Xin
    Posts: 3,251
    You should comment your code so for people that may not understand the OOP ;)
    Xin
  • chroniccommand
    Posts: 1,389
    said:


    You should comment your code so for people that may not understand the OOP ;)



    That's why I included those links but will do. Just gimme a sec :P
  • Xin
    Posts: 3,251
    said:


    said:


    You should comment your code so for people that may not understand the OOP ;)



    That's why I included those links but will do. Just gimme a sec :P


    Cheers man, im not too good on OOP and really wanna learn it.
    Xin
  • chroniccommand
    Posts: 1,389
    said:


    said:


    said:


    You should comment your code so for people that may not understand the OOP ;)



    That's why I included those links but will do. Just gimme a sec :P


    Cheers man, im not too good on OOP and really wanna learn it.


    Check out the links in the original post. They really helped me. To actually learn the concepts of OOP check this out:
    http://download.oracle.com/javase/tutor ... /concepts/
  • McKittrick
    Posts: 194
    in looking at java in the past, i think it is one of the most painfully difficult forms of code to learn. i will even take ASM over java anyday!