0

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 :)

smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    In Python, unnecessary accessors are frowned upon. These are a Java idiom—there's just no need for a function like `get_rank`. Use `card1.rank`, and make it a property if you ever require a non-trivial accessor function. – user3426575 Nov 23 '14 at 02:17
  • [Python standard convention](http://stackoverflow.com/questions/5978557/association-between-naming-classes-and-naming-their-files-in-python-convention) recommends: since your file defines *Card* class (and/or Deck, Hand and so on), call it *card.py*. Never call it file.py, like @HaiVu says. – smci Nov 23 '14 at 03:00
  • I'll keep in mind not to use "file.py" anymore. :) – Jirka Mára Nov 23 '14 at 19:49

1 Answers1

3

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.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93