Is it possible to get undefined behavior in python? And if possible, how?

2 answers 2

The bytecode module is required (to generate bytecode).

import types import bytecode from bytecode import ConcreteInstr code = bytecode.Bytecode() code.append(ConcreteInstr('POP_TOP')) stop_signal = types.FunctionType(code.to_code(), {}) stop_signal() #в доках питона не сказано что здесь произойдет сегфолт.(хотя это известно) 

    Python gives out 10, in it there is no increment in such type:

      i = 5 i = ++i + ++i print i class Foo: def __init__(self, num): self.num = num def inc(self): self.num += 1 return self.num i = Foo(5) print(i.inc() + i.inc()) 

    because the interpreter calculates everything in order. But if you think a bit and bring everything to a logical mind, then all the same 14

     class Foo: def __init__(self, num): self.num = num def inc(self): self.num += 1 return self def __add__(self, right): return Foo(self.num + right.num) def __repr__(self): return repr(self.num) i = Foo(5) print(i.inc() + i.inc()) 
    • What is the undefined behavior? - insolor
    • @insolor code ++ i is generally a pre-increment, and it should be 6, 6 + 6 = 12, gives 10 .... Behavior is not defined. - Mikhail Alekseevich
    • @ MikhailAlekseevich, in Python there is no such increment at all (neither pre-post-). No increment - no NP associated with it. - insolor
    • @insolor yes, man confused C with python :) A sort of trolling. - Mikhail Alekseevich
    • Well, there is a little) But the answer is on the same topic. vaguely! - Philip Pilipchuk