reegan a
reegan a

Reputation: 45

Class with in class in python

I have a classex.py file:

class Class1:
    def method1():
        print 'stack1'
    def method2():
        print 'over1'
    def method3():
        print 'flow1'

class Class2:
    def method1():
        print 'stack2'
    def method2():
        print 'over2'
    def method3():
        print 'flow2'

class Class3:
    def method1():
        print 'stack3'
    def method2():
        print 'over3'
    def method3():
        print 'flow3'

I want access classex file class so I will import that file how to access in various classes .

For example:

import classex
print Class1.method1()
print Class2.method2()

my expect output is stack1 and over2

Upvotes: 0

Views: 212

Answers (3)

vlad-ardelean
vlad-ardelean

Reputation: 7622

Or you can do this

class Class1:
    @classmethod
    def method1(clss):
        print 'stack1'
    @classmethod
    def method2(clss):
        print 'over1'
    @classmethod
    def method3(clss):
        print 'flow1'

class Class2:
    @classmethod
    def method1(clss):
        print 'stack2'
    @classmethod
    def method2(clss):
        print 'over2'
    @classmethod
    def method3(clss):
        print 'flow2'

class Class3:
    @classmethod
    def method1(clss):
        print 'stack3'
    @classmethod
    def method2(clss):
        print 'over3'
    @classmethod
    def method3(clss):
        print 'flow3'

and then

import classex

classex.Class1.method2()

etc.

But what you're doing doesn't really make me feel you understood the OOP principles that well, or that you're comming from the another OOP language that has some king of class-methods and you'd expect to get the same result. Python implementation of OO differs slightly from Java or C# or the like, so make sure to read a little on how methods are defined in Python.

Upvotes: 0

TerryA
TerryA

Reputation: 59974

You didn't actually import the Classes themselves but just the module.

import classex

In order to access the classes you would have to do:

>>> myclass = classex.Class1()
>>> myclass2 = classex.Class2()
>>> myclass.method1()
stack1
>>> mycalss2.Class2.method2()
over2

Or import the classes directly:

>>> from classex import Class1, Class2
>>> myclass = Class1()
>>> myclass2 = Class2()
>>> myclass.method1()
stack1
>>> myclass2.method2()
over2

Upvotes: 2

jamylak
jamylak

Reputation: 133554

class Class1:
    def method1(self): # instance  is passed implicity as first arg
        print 'stack1'
    def method2(self):
        print 'over1'
    def method3(self):
        print 'flow1'

class Class2:
    def method1(self):
        print 'stack2'
    def method2(self):
        print 'over2'
    def method3(self):
        print 'flow2'

class Class3:
    def method1(self):
        print 'stack3'
    def method2(self):
        print 'over3'
    def method3(self):
        print 'flow3'

>>> import classex
>>> c = classex.Class1() # instance of Class1
>>> c.method1()
stack1

Upvotes: 5

Related Questions