高校化学2924392 views
りんご209490 views
中学理科1630641 views
中学英語811710 views
雑学1473612 views
いろは3010814 views
世界の国564520 views
ヒストリア290598 views
高校生物551766 views
高校倫理1440106 views
Help
Tools
NewsSpreadsheetCalendarBookkeepingMarkdown TablesSlidesTier ListPen ToolIllustrationCrayonWatercolorPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchSwirl EffectLine ArtOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksWater RippleWater SplashBreaking GlassWood GrainMarble TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsRolling DiceCross SectionsMotion PathMechanicsWavesElectromagnetic WavesCapacitorsLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHOrganic Reaction MapPeriodic TableComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseMuscle ContractionHormones and HomeostasisRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic TheoryShogi StrategyPiano Rhythm Game

English

Python のコードオブジェクト(__code__)の詳細解析

関数の __code__ 属性はコードオブジェクトを保持しており、関数の内部構造に関する詳細情報を提供します。

コードオブジェクトの主要属性

def example(a, b, *args, c=10, **kwargs):
    x = a + b
    y = c * 2
    return x + y

code = example.__code__
co_name関数名
co_filename定義されたファイル名
co_firstlineno定義開始行番号
co_argcount位置引数の数(*args を除く)
co_kwonlyargcountキーワード専用引数の数
co_varnamesローカル変数名のタプル
co_freevars自由変数(クロージャで参照)
co_cellvarsセル変数(内部関数に渡す)
co_consts定数のタプル
co_codeバイトコード

具体的な値を確認

print(code.co_name)           # example
print(code.co_argcount)       # 2 (a, b)
print(code.co_kwonlyargcount) # 1 (c)
print(code.co_varnames)       # ('a', 'b', 'c', 'args', 'kwargs', 'x', 'y')
print(code.co_consts)         # (None, 10, 2)

クロージャと自由変数

def outer(x):
    def inner(y):
        return x + y
    return inner

closure = outer(10)
print(closure.__code__.co_freevars)  # ('x',)

co_freevars には、外側のスコープから参照している変数が記録されます。

コードオブジェクトの置き換え

コードオブジェクトを操作して関数の挙動を変えることも可能です(非推奨ですが理解として)。

def original():
    return 1

def replacement():
    return 2

original.__code__ = replacement.__code__
print(original())  # 2

実用的な活用

def get_function_info(func):
    code = func.__code__
    return {
        "name": code.co_name,
        "args": code.co_varnames[:code.co_argcount],
        "file": code.co_filename,
        "line": code.co_firstlineno,
    }

コードオブジェクトの理解は、デバッガーやプロファイラの開発、動的コード生成に役立ちます。