1

on (1) 最初の行で関数を定義します。
on (2) 5 行目で同じ関数を定義します。

どちらが速いですか?必要なときに「近い」関数を定義することは、大規模なプログラムの複雑さのために実用的ではありません(ただし、高速になります)。または、必要な場所から「遠くに」関数を定義すると、実行時に大規模なプログラムが遅くなります(ただし、より実用的です)。

私はパイソンを使用しています。もちろん、ここでの違いはおそらくナノ秒単位ですが、これは単なる例です。

1-

def t(n1,n2):
    v=n1-n2
    return abs(v)
a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()

2-

a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

def t(n1,n2):
    v=n1-n2
    return abs(v)

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()
4

2 に答える 2

2

どちらも同じです。定義する前に関数を呼び出さない限り (または変数などを使用しない限り)、どこに配置しても問題ありません。いずれにせよ、すべてのコードは Python インタープリターによって最適化され、関数が定義されている関数呼び出しからどれだけ離れているかは気にしません。

于 2013-09-01T03:13:51.193 に答える
0

関数は、呼び出す前に解析されます。したがって、どこで定義したかは問題ではありません。

于 2013-09-01T03:29:47.800 に答える