라벨이 내장함수인 게시물 표시

파이썬[Python]: 내장함수 - __setattr__ 메서드

파이썬(Phthon): 내장함수 - __setattr__ 메서드(method) /// 설명 클래스의 속성(attribute)에 값을 할당하고자 할 경우 호출 됩니다. ※ 형식 object.__setattr__(self, name, value) reference https://docs.python.org/3/reference/datamodel.html#object.__setattr__ /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class  A:      def  __init__( self , a):          self .a  =  a        def  __setattr__( self , attr, value):         value  =  f 'attribute: {attr}, value: {value}'          super ().__setattr__(attr, value)          # object.__setattr__(self, attr, value)     a  =  A( 'String' ) print (a.a)   # attribute: a, value: String a.a  =   'a' print (a.a)   # attribute: a, value: a   # create new attribute a.b  =   'b' print (a.b)   # attribute: b, value: b   # create new attribute setattr(a,  'c' ,  'c' ) print (a.c)   # attribute: b, value: b   # create new attribute a.__dict__[ 'd' ]  =   'd' print (a.d)   # d print (a._

파이썬[Python]: keyword - nonlocal 키워드

내장함수 - nonlocal 키워드(keyword) /// 설명 중첩함수(nested function)에서 상위 함수의 변수 내용을 변경(encapsulated code) 할 수 있습니다. ※ 형식 nonlocal reference https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-nonlocal_stmt /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 def  func_a():     a  =   "a_string"      def  func_b():         a  =   "func_a_string"      print (a)   # a_string     func_b()      print (a)   # a_string     func_a()   def  func_c():     b  =   "b_string"      def  func_d():         nonlocal b         b  =   "func_b_string"      print (b)   # b_string     func_d()      print (b)   # func_b_string     func_c()   cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: keyword - raise 키워드

내장함수 - raise 키워드(keyword) /// 설명 예외(오류)를 발생시킵니다. 실행 후 차례로 주석처리하시면서 확인해 보시기 바랍니다. ※ 형식 raise reference https://docs.python.org/3/tutorial/errors.html https://docs.python.org/3/library/exceptions.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 raise # Traceback (most recent call last): #   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 1, in <module> #     raise # RuntimeError: No active exception to reraise     raise  Exception # Traceback (most recent call last): #   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 8, in <module> #     raise Exception # Exception     raise  Exception( 'Write Exception' ) # Traceback (most recent call last): #   File "D:\Psychedelphia\Computer_Program\Py\Practice\main.py", line 14, in <module>

파이썬[Python]: keyword - assert 키워드

내장함수 - assert 키워드(keyword) /// 설명 디버깅의 편리성을 위해 사용합니다.(표현(expression)이 False 일 경우 오류를 발생시킵니다.) ※ 형식 assert reference https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-assert_stmt /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 a  =   True assert a   if  a:      print ( 'Not Error' ) else :      print ( 'means Error' )   b  =   1 assert b  > =   0 # assert b < 0, 'b >= 0'  # AssertionError   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: keyword - pass 키워드

내장함수 - pass 키워드(keyword) /// 설명 아무런 동작도 하지 않을 경우 사용합니다. ※ 형식 pass reference https://docs.python.org/3/reference/simple_stmts.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 def  func_a():  print ( 'function test' )     func_a()   # function test     def  func_b():  pass     # nothing is printed func_b()   #     class  A:      def  func( self ):  print ( 'class A test' )     a  =  A() a.func()   # class A test     class  B:      def  func( self ):  pass     b  =  B() # nothing is printed b.func()   #     class  C:  pass   c  =  C()   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: keyword - not 키워드

내장함수 - not 키워드(keyword) /// 설명 논리값을 부정합니다.(참->거짓, 거짓->참) ※ 형식 not reference https://docs.python.org/3/reference/expressions.html#not /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 a  =   True print (a)   # True print ( not  a)   # False   b  =   'string' print (b)   # string print ( not  b)   # False print ( not   not  b)   # True   c  =   '' print (c)   # print ( not  c)   # True print ( not   not  c)   # False   d  =   1 print (d)   # 1 print ( not  d)   # False   if  b  is   not  c:      print ( 'b is string' )   # b is string else :      print ( 'empty' )   print ( 'b is string'   if  b  is   not  c  else   'empty' )   # b is string   print ( 's'   in   'string' )   # True print ( 's'   not   in   'string' )   # False   print ( 'In'   if   's'   in   'string'   else   'None' )   # In print ( 'None'  

파이썬[Python]: keyword - or 키워드

내장함수 - or 키워드(keyword) /// 설명 하나 이상의 값이 참일 경우 참(True)을 반환합니다.(모든 값이 False 일 경우 False) ※ 형식 or reference https://docs.python.org/3/reference/expressions.html#or /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 a  =   False b  =   False   x  =  a  or  b   print (x)   # False   if  x:      print ( 'x is True' ) else :      print ( 'x is False' )   # x is False   c  =   True   y  =  a  or  b  or  c   print (y)   # True   print ( 'y is True'   if  y  else   'y is False' )   # y is True # test = 'y is True' if y else 'y is False' # print(test)   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: keyword - and 키워드

내장함수 - and 키워드(keyword) /// 설명 모든 값이 참일 경우 참(True)을 반환합니다. ※ 형식 and reference https://docs.python.org/3/reference/expressions.html#and /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 a  =   True b  =   True   x  =  a  and  b   print (x)   # True   if  x:      print ( 'x is True' )   # x is True else :      print ( 'x is False' )   c  =   False   y  =  a  and  b  and  c   print (y)   # False   print ( 'y is True'   if  y  else   'y is False' )   # y is False # test = 'y is True' if y else 'y is False' # print(test)   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

파이썬[Python]: 내장함수 - super 함수

내장함수 - super 함수(function) /// 설명 상위(parent) 클래스나 등위(sibling) 클래스의 메서드 호출을 위임하는 객체(중계기능)를 반환합니다.(속성(attribute)에도 사용할 수 있습니다.) super() 는 __mro__ 에서 반환하는 우선순위 중, 상위 클래스에서 처음나오는 메서드를 호출하며, 모든 상위 클래스에 메서드가 존재하지 않으면 오류를 발생시킵니다. ex) class A -> class B -> class C -> object class A에서 super()를 사용할 경우 B, C, object 순으로 탐색을 하고, 그 중 가장 먼저 나오는 메서드를 호출합니다. 만약 없으면 오류를 발생시킵니다. 클래스 내부에서 사용시 상위 클래스가 단일하다면, super().method() 형태로 사용하시면 되고, 상위 클래스가 둘 이상이라면, super(type, object).method() 형태로 사용하시면 됩니다. 참고: __mro__ ※ 형식 class super([type[, object-or-type]]) reference https://docs.python.org/3/library/functions.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class  A(object):   # base class A      def  a( self ):          print ( 'test A' )     class  B(A):      def  a( self ):          print ( 'test B' )     if  issubclass(B, A):      print (A.__mro__)   # (<class '__main__.A'>, <class 'object'>)      print (B.__m

파이썬[Python]: 내장함수 - __mro__ 변수

파이썬(Phthon): 내장함수 - __mro__ 변수(variable) /// 설명 상위 클래스의 메서드를 탐색하는데 사용되어집니다. 상위 클래스에 같은 이름을 가진 메서드가 있을 경우 우선하여 시행하는 순서를 보여줍니다. ex) (&ltclass '__main__.D'>, &ltclass '__main__.B'>, &ltclass '__main__.A'>, &ltclass 'object'>) D -> B -> A -> object ※ 형식 __mro__ reference https://docs.python.org/3/library/stdtypes.html#class.__mro__ /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class  A(object):      def  a( self ):          print ( 'test A' )     print (A.__mro__)   # (<class '__main__.A'>, <class 'object'>)     class  B(object):      def  a( self ):          print ( 'test B' )     print (B.__mro__)   # (<class '__main__.B'>, <class 'object'>)     class  C(A, B):      pass     print (C.__mro__)   # (<class '__main__.C'>, <class '__main__.A'>, <class '__mai

파이썬[Python]: 내장함수 - type 함수

내장함수 - type 함수(function) /// 설명 객체의 타입을 반환하며, object.__class__ 와 같은 값을 가집니다. 인자 3 개를 사용할 경우에는 새로운 객체를 생성합니다. 참고: isinstance() ※ 형식 class type(object) class type(name, bases, dict, **kwds) reference https://docs.python.org/3/library/functions.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import  types     def  func_a():      pass     class  A:      pass     test_1  =   1 test_2  =   'string' test_3  =  [ 1 ,  2 ,  3 ,  4 ,  5 ] test_4  =  lambda:  1   print (type(test_1))   # <class 'int'> print (type(test_2))   # <class 'str'> print (type(test_3))   # <class 'list'> print (type(test_4))   # <class 'function'> print (type(func_a))   # <class 'function'> print (type(A()))   # <class '__main__.A'> print (test_1.__class__)   # <class 'int'> print (test_2.__class__)