How to pass a class/module definition into another file

2.2k views Asked by At

I'm a beginner in OOP Python and I just wanna know this:

This is file.py:

class Card():

    def __init__(self, rank, suit):
        """Initialization method"""
        self.rank = rank
        self.suit = suit

    def get_rank(self):
        """Function get_rank returns a rank (value) of the card"""
        return self.rank

When I wanna create and pass an object to the function "get_rank" in this file, I can do this:

card1 = Card(10,"Diamond")
card1.get_rank()

But how can I create and pass an object in another file? There is another file called test_file.py, it's a testing file (py.test). So file.py is for code only, test_file.py represents parameters (variables and objects) which are passed to file.py. In test_file.py is also variable "expected_result" with a correct result. Then when I use "py.test" (bash), it shows me, if the result is correct or not.

I understand this non-OOP example: abs.py:

def split_text(text): - code -

test_abs.py:

def test():
    text = 'abcdefgh'

Please help, thanks for any advice :)

1

There are 1 answers

1
Hai Vu On BEST ANSWER

In your test_file.py:

from file import Card

...
card1 = Card(10, "Diamond")
# Do something with it

By the way, do not name your file file.py: file is a built-in function.