1

I am kind of stuck in the a middle of a personal project that I am doing. I have a scenario which is just not making any sense to me nor do I know what to "call it" so I am also not finding the right answers from internet.

If you can give a solution for me for the following example I would really appreciate it.

I am trying to do the following:

I have two classes/modules in different file(later maybe also adding inside sub directory as well).

client.py

class client(object):
    def __init__(self, user, key, address):
        self.user = user
        self.key = key
        self.address = address

foo.py

class foo(client):
    print(client.user, client.key, client.address)

Now I want to call foo() by initiating client() class first. So my test.py file should look like this and it should print those values I have given when initiating client class.

test.py

import client
f = client("x", "passkeyXX","10.192.0.1")
f.foo() ## should print the values given above

is this possible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • the init needs a self reference, have you checked if your import actually works? You might need to call it with client.py and a path to it depending on where you saved it – Eumel Nov 06 '17 at 09:20
  • Sounds like you are trying to do something similar to this --> [https://stackoverflow.com/q/46996982/7345804](https://stackoverflow.com/q/46996982/7345804) –  Nov 06 '17 at 09:37

2 Answers2

0

You can make this within one class, while not creating both:

class Client(object):
    def __init__(self, user, key, address):
        self.user = user
        self.key = key
        self.address = address

    def foo(self):
        print(self.user, self.key, self.address)

Then you instantiate it in tests:

c = Client(user='user1', key='sdf234sdf13', address='USA, New York')

and call the needed method foo:

c.foo()

which should return you:

user1 sdf234sdf13 USA, New York

UPDATE:

In client.py

from foo import foo as _f

class Client(object):
    def __init__(self, user, key, address):
        self.user = user
        self.key = key
        self.address = address

    def foo(self):
        return _f(self.user, self.key, self.address)

In foo.py:

def foo(user, key, address):
   print(user, key, address)

Now you can import your class Client somewhere in tests.

from client import Client

Make instance:

c = Client(user='user1', key='sdf234sdf13', address='USA, New York')

call c.foo() and it should print out what you need, but this is so weird though

py_dude
  • 822
  • 5
  • 13
  • Thanks for the answer. But I am afraid I am not really looking for that. I can do what you have suggested by writing methods for the client class in the same file. But thats not what I want. I need to write the foo() in different file. – user3800017 Nov 07 '17 at 01:01
  • I need to make the class foo() subclass of client class but i need to make separate file for foo(). The reason I am asking is because I have several ( more then 50 classes) foo() like classes that would reside in my module directory. – user3800017 Nov 07 '17 at 01:46
  • You see, this, what you are talking about, is not subclassing and it's not logical. "foo" cannot be a class, but I can make this working like function of class Client and be called as "c.foo()". Do you want me to do this? Feel free to ask questions. – py_dude Nov 07 '17 at 06:32
  • Yes. I see the mistake. But the scenario still wont change. All I want is to have foo() in a separate file from the client.py and still be part of client() as a method or a subclass. If you can give me that kind of example it would be really really great! Thanks!!! – user3800017 Nov 08 '17 at 04:26
0

To initialise a child class based on one of its parents, you can call the super method

Method 1: in the __init__ method

class foo(client):
    def __init__(self):
        super(self, client).__init__(self, user, key, address)

Method 2: on specific object creation

bar = foo().super(self, client).__init__(<user>, <key>,< address>) 

Check out the docs for details.

Xero Smith
  • 1,968
  • 1
  • 14
  • 19