classes in python

This is the place for queries that don't fit in any of the other categories.

classes in python

Postby siestamadokaist » Fri Apr 19, 2013 6:29 pm

sorry for noobing, I'm intended to create an OOP program with python, but the documentation has too many explanation haha.
so...could anyone just give me an example of how do this pseudocode, translated in python class:
Code: Select all
class testClass extends someParentClass{
   var private a;
   var private b;
   var private c;
   new(a,b){ #instatiate a new class
      this.a = a;
      this.b = b;
   }
   setC(c){ #ignore where the set A and set B, this is just an example anyway
      this.c = c;
   }
   getB(){
      return this.b;
   }
   inheritedFunctionFromParent(){
      #doSomething()
   }
}

And then this is the main program
Code: Select all
class MainProgram{
#do I need to start this as a class that runs its "Main" function, or just as the usual procedural python?
   Main(){
      x = new testClass('a','b');
      x.setC('a');
   }
}
Last edited by siestamadokaist on Fri Apr 19, 2013 6:43 pm, edited 1 time in total.
siestamadokaist
 
Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

Re: classes in python

Postby setrofim » Fri Apr 19, 2013 6:38 pm

siestamadokaist wrote:but the documentation has too many explanation haha.

There is enough to explain how classes work. You should read it; don't be impatient. A large part of programming is reading documentation. If you don't understand something, post specific questions.
siestamadokaist wrote:so...could anyone just give me an example of how do this pseudocode, translated in python class:
Code: Select all
class testClass{
   var private a;
   var private b;
   var private c;
   new(a,b){ #instatiate a new class
      this.a = a;
      this.b = b;
   }
   setC(c){ #ignore where the set A and set B, this is just an example anyway
      this.c = c;
   }
   getB(){
      return this.b;
   }
}

Code: Select all
class TestClass(object):

    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = None


  • There are no private variables in Python (well, there are, sort of, but you generally shouldn't use them).
  • Don't do the whole getter/setter thing in Python. Just access the member variables you want directly.
  • Unlike some other languages, Python does not have a this keyword. Instead, the reference to a class instance is passed as the first argument to the method (conventionally called "self").
  • In most cases, you should provide initialisation by overriding the __init__() method of the the class

EDIT:

siestamadokaist wrote:And then this is the main program
Code: Select all
class MainProgram{
#do I need to start this as a class that runs its "Main" function, or just as the usual procedural python?
   Main(){
      x = new testClass('a','b');
      x.setC('a');
   }
}

Code: Select all
if __name__ == '__main__':
    x  = TestClass('a', 'b')
    x.c = 'a'

Do not enclose the top level code in a class; this ain't Java. The if statement is used to prevent accidental execution of top level code when the .py file is imported as a module.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: classes in python

Postby siestamadokaist » Fri Apr 19, 2013 7:00 pm

Code: Select all
#file TestClass.py
class TestClass(object):
   def __init__(self,a,b):
      self.a = a;
      self.b = b;
      self.c = None;


Code: Select all
#file mainProgram.py
import TestClass
if __name__ == '__main__':
   x = TestClass('a','b')
   x.c = None;

both in the same directory,
but I get this error:
Code: Select all
Traceback (most recent call last):
  File "D:\python\personal\frozen_neutrino\trunk\FnMainProgram.py", line 3, in <module>
    x = TestClass('a','b')
TypeError: 'module' object is not callable
[Finished in 0.1s with exit code 1]

did I miss something? or I need some lib?
siestamadokaist
 
Posts: 12
Joined: Sun Mar 31, 2013 4:48 pm

Re: classes in python

Postby setrofim » Fri Apr 19, 2013 7:04 pm

The import statement should be
Code: Select all
from TestClass import TestClass

In Python, each module (.py file) has its own namespace. Dude, seriously, go through the tutorial. Also, get out of the habit of sticking each class in its own file (again, Python ain't Java).
.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests