6 Replies - 8309 Views - Last Post: 08 September 2010 - 12:31 PM Rate Topic: -----

#1 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Calling classes in Python

Posted 08 September 2010 - 10:45 AM

Anyone who is familiar with Java knows that you can make one class call another class. To do this you would use:
Test dummy = new Test();
(That may be slightly off, I am not that well versed in Java).

My question is this: Is there anything similar to this in Python? I like having more than one class as it makes everything more organized. So what say you? How does Python compare on this front?
Is This A Good Question/Topic? 0
  • +

Replies To: Calling classes in Python

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5821
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: Calling classes in Python

Posted 08 September 2010 - 10:59 AM

Python doesn't explicitly name variable types. However, classes are easy enough. The call would generally look like:
test = Test()



If you like, throw out some real Java code and I could throw back some real Python code. However, there are numerous resources online for python. It's one of it's charms.
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Calling classes in Python

Posted 08 September 2010 - 11:03 AM

Are you talking about a class factory?

class Factory:
   def build(self, type):
      if type == 'queue':
         import Queue
         return Queue.Queue()
      elif type == 'list':
         return []

factory = Factory()
mylist = factory.build('list')
myqueue = factory.build('queue')



If this is not what you are looking for, would you mind clarifying your question.
Was This Post Helpful? 0
  • +
  • -

#4 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Calling classes in Python

Posted 08 September 2010 - 11:18 AM

To clarify, using an example:

classA.java holds runThis(int x).
classB.java calls runThis(int x) from classA.java.

In java the code classA varname = new classA(); is used to call classA so all of the objects of classA can be accessed. Then you would use varname.runThis(int x) in classB.

Simply put, all of these different classes work together, calling on each other as needed. This makes the program faster since it doesn't have to sift through unneeded code. It also makes things more organized.

Make sense?

This post has been edited by .i7: 08 September 2010 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5821
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: Calling classes in Python

Posted 08 September 2010 - 12:08 PM

View Post.i7, on 08 September 2010 - 12:18 PM, said:

Simply put, all of these different classes work together, calling on each other as needed. This makes the program faster since it doesn't have to sift through unneeded code. It also makes things more organized.

Make sense?


Not as much as you think, I suspect. ;)

The "makes the program faster" bit is incorrect, in any language. Organization, of course, is always a goal, regardless of tool.

Python uses classes, but they're not the requirement they are in Java. It's not less OO so much as different OO. Types are loose. Everything is an object. The properties of an object aren't really important until the program asks for them. Even functions are objects and in many cases that's all you'll need.

An instance calling an instance? Sure:
class ClassA:
	def runThis(self, x):
		print("result = ", x*x)

class ClassB:
	def runThis(self, x):
		print("Class B calling ClassA")
		a = ClassA()
		a.runThis(x)

instance = ClassB()

instance.runThis(5)


Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Calling classes in Python

Posted 08 September 2010 - 12:16 PM

I think we may have run into a terminology/language structure difference that is causing problems in communication.

What I believe you are trying to do is incorporate code that lies in different files. Doing this in python requires using the import command.

An example involving two files, main.py and sub.py follows.

main.py
import sub

myclass = sub.MyClass()
myclass.Shout()

sub.PrintNicely()



sub.py
class MyClass():
    def Shout(self):
        print "INSIDE SUB.MYCLASS.SHOUT()!!!"

def PrintNicely():
    print "Inside sub.PrintNicely()."


Was This Post Helpful? 2
  • +
  • -

#7 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Calling classes in Python

Posted 08 September 2010 - 12:31 PM

That is exactly what I was asking for Motoma. I apologize for my terminology, it isn't exactly up to par! Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1