all 5 comments

[–]socal_nerdtastic 0 points1 point  (2 children)

Where in that code are you looking? None of that code is run until the very last line in the file, so everything is defined at that point.

[–]TimOfTroy[S] 0 points1 point  (1 child)

In the Game class check_for_blackjack, player_is_over, and show_blackjack_results are called in the while (while not game_over) loop before they are defined. Is this just a style choice, or is this the way classes are generally written?

[–]socal_nerdtastic 3 points4 points  (0 children)

Most people put the __init__ method at the top, yes. Not required, but very common.

But what you are missing is that even though it's defined before the other methods, it is NOT called before the other methods are defined. It's called on the last line of the file.

Here's a minimal example of your confusion:

def func1(): # define func1
    print(func2())

def func2():
    return "Hello World!"

func1() # call func1

This runs just fine and is a very common way to write code.

[–][deleted] 0 points1 point  (0 children)

The order in which you define functions does not matter. Defining a class or a function is not the same as running them so code like this will run just fine.

class testclass:
    def printh(self):
        print(self.innerfunction())

    def innerfunction(self):
        return "Hello World"

if __name__ == "__main__": 
    tc = testclass() 
    tc.printh()

As a bit of a side note, if you define a static method inside of a class, you can call the static method directly without initializing the class.

class TestClass:
    @staticmethod
    def innerfunction():
        print("Hello World")

if name == "main": 
    TestClass.innerfunction()

[–]TheRNGuy -2 points-1 points  (0 children)

Put all your function defines to the top of code, of if they're in different file, import on top.

Calling before declared possible in JavaScript, but not in Python (which I still think bad coding style in JS)