Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Question about OOP in Python
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Exci
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 265
Location: The Netherlands, Zoetermeer

PostPosted: Wed Jul 30, 2003 6:36 pm    Post subject: Question about OOP in Python Reply with quote

I have coded some apps in java and I really like the way of java coding.
But I wanted to learn more 'languages' so I started learning Python some weeks ago, I must say that I like it, I started coding something for xchat to get to know the language a bit, I coded a '_OW SO ANNOYING_' mp3script with a mysql backend (for recording of mp3 played/skipped/finished topX bandtopX etc) I did that in 2 scripts, one for xchat and 1 for CLI (that fixes the database if needed).

But I never used classes, until I tried to make a IRC client to try socket programming a bit. but I want (just as I would do in java) to give every class i make its own file, and store that in the same(or sub)dir as the 'starter' is in. how does this work in python? can someone give me an example ?

ps, I looked in the python tutorial on python.org but I couldn't find anything about it :?
Back to top
View user's profile Send private message
compu-tom
Guru
Guru


Joined: 09 Jan 2003
Posts: 415
Location: Berlin, Germany

PostPosted: Wed Jul 30, 2003 7:43 pm    Post subject: Reply with quote

Assumed you have in file MyClass.py:
Code:

# declare the class "MyClass"
class MyClass:
    # The constructor has 2 args which are stored in instance variables
    def __init__(self, firstArg, secondArg):
        self.firstArg = firstArg
        self.secondArg = secondArg

    def someMethod(self):
        print "someMethod called " + self.firstArg

Then a subclass of MyClass, MySubclass:
Code:

# Import all symbols from the modules MyClass into the
# current namespace.
from MyClass import *

# Declare MySubclass as subclass of MyClass
class MySubclass(MyClass):
    # The constructor has 3 args, 2 of them are passed to the super
    #constructor and the third is stored in an instance variable of
    # MySubclass
    def __init__(self, firstArg, secondArg, thirdArg):
        MyClass.__init__(self, firstArg, secondArg)
        self.thirdArg = thirdArg

    def doSomeThing(self, what):
        self.someMethod()
        return "Done something " + what

And in MyOtherClass.py:
Code:

# This is an alternative way for import: Don't copy the symbols to the
# current namespace. This requires to fully specify the module's name
# and the actual symbol from the module to access it (see below)
import MySubclass

class MyOtherClass:
    def __init__(self):
        self.someClass = MySubclass.MySubclass("abc", 123, "456")

    def doAnyThing(self):
        print self.someClass.doSomeThing("very important")


This code comes without warranty and untested :)
Hope this helps...
Back to top
View user's profile Send private message
Exci
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 265
Location: The Netherlands, Zoetermeer

PostPosted: Wed Jul 30, 2003 8:40 pm    Post subject: Reply with quote

ah thank you for making such clear examples :D, I only tried the 'import myClass' and forgot the 'MySubclass.' in front of it :) so that's why I failed earlier :)

I just read the 'whats new in 2.3' and learned about the -O switch
I tried it and it made a .pyc does the 'c' mean (pre)compiled? and I found alot of .pyo in /usr/lib/python dir, o stands for object?

what's the difference between o and c ?
can you give me a small example?
Back to top
View user's profile Send private message
compu-tom
Guru
Guru


Joined: 09 Jan 2003
Posts: 415
Location: Berlin, Germany

PostPosted: Wed Jul 30, 2003 8:51 pm    Post subject: Reply with quote

File *.py are the source files,
*.pyc are the byte code compiled files (similar to *.class in java),
*.pyo are byte code files with basic optimization, according to "man python":
Quote:
-O Turn on basic optimizations. This changes the filename exten-
sion for compiled (bytecode) files from .pyc to .pyo. Given
twice, causes docstrings to be discarded.


I never used the optimization flag. So I can't tell you if it makes a difference regarding performance.
Back to top
View user's profile Send private message
Exci
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 265
Location: The Netherlands, Zoetermeer

PostPosted: Wed Jul 30, 2003 9:26 pm    Post subject: Reply with quote

ah, well I can play/code on with allot more overview now
and I'll play around withthe -O flag a bit :)


ty again
Back to top
View user's profile Send private message
thraxil
n00b
n00b


Joined: 04 Apr 2003
Posts: 46
Location: nyc

PostPosted: Thu Jul 31, 2003 3:08 pm    Post subject: Reply with quote

since it looks like you're already a programmer, Mark Pilgrim's "Dive Into Python" tutorial (http://diveintopython.org/) should be very useful for you.
Back to top
View user's profile Send private message
Exci
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 265
Location: The Netherlands, Zoetermeer

PostPosted: Thu Jul 31, 2003 3:34 pm    Post subject: Reply with quote

thx, added to my bookmarks :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum