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

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

파이썬(Phthon): 내장함수 - __bases__ 변수(variable) /// 설명 상위 클래스들을 포함하는 튜플을 반환합니다. ※ 형식 __bases__ reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import  array   print (array.array.__bases__)   # (<class 'object'>,)     class  A(object):      pass     print (A.__bases__)   # (<class 'object'>,)   class  ASub(A, array.array):      pass     print (ASub.__bases__)   # (<class '__main__.A'>, <class 'array.array'>)   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

내장함수 - vars 함수(function) /// 설명 모듈, 클래스, 인스턴스 또는 다른 어떤 객체의 __dict__ 값을 반환합니다. ※ 형식 vars([object]) 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 import  __main__     class  A:      def  __init__( self , string):          self .string  =  string     print (vars())   # {'__name__': '__main__', '__doc__': None, '__package__': None, ... print (locals())   # {'__name__': '__main__', '__doc__': None, '__package__': None, ... print (vars(__main__))   # {'__name__': '__main__', '__doc__': None, '__package__': None, ... print (__main__.__dict__)   # {'__name__': '__main__', '__doc__': None, '__package__': None, ...   a  =  A( 'This is a string' ) print (vars(a))   # {'string': 'This is a string'} print (a.__dict__)   # {'string': 'This is a string'}   Col

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

파이썬(Phthon): 내장함수 - __getattr__ 메서드(method) /// 설명 기본 속성에 접근할 수 없을 경우 호출 됩니다. 반환 값은 (변형된)속성값이어야 합니다. 어떤 속성이든(존재하던, 존재하지 않던) 접근할 경우 __getattribute__() 호출, 없을 경우 __getattr__() 호출합니다. __getattribute__() 은 일반적 방식입니다. 이 방식이 작동하지 않으면 __getattr__()는 __dict__() 를 다시 찾아봅니다. 그리고 속성이 있다면 반환하고 없으면 (변형된)반환값을 표현하거나 오류를 발생시킵니다. 참고: __getattribute__() ※ 형식 object.__getattr__(self, name) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 '' '     Just a moment... if this method has no special work to do, it is useless ' ''   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __getattribute__ 메서드(method) /// 설명 클래스의 인스턴스에서 속성(attribute)에 접근하고자 할 경우 호출 됩니다. 클래스의 속성을 직접 반환하면 recursion 이 발생합니다. object.__getattribute__(self, name) 필요 ex) a.string -> __getattribute__ 호출 -> return self.string -> __getattribute__ 호출 -> return self.string -> ... 참고: __getattr__() ※ 형식 object.__getattribute__(self, name) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class  A:      def  __init__( self , string):          self .string  =  string        def  __getattribute__( self , name):          try :             attribute  =   super (A,  self ).__getattribute__(name)              return  attribute          except  AttributeError  as  e:              print (e)     a  =  A( 'This is a string' ) print (a.__dict__)   # {'string': 'This is a string'} print (a.string)   # This is a string print (getattr(a,  'string' ))   # This is a 

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

파이썬(Phthon): 내장함수 - __bool__ 메서드(method) /// 설명 True 값을 테스트하고 내장함수 bool() 을 구현하기 위해 호출 됩니다. 참고: __len__() ※ 형식 object.__bool__(self) reference https://docs.python.org/3/reference/datamodel.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 class  A:      def  __init__( self , a):          self .a  =  a        def  __bool__( self ):          return  bool( self .a)     print (A( '' ).__bool__())   # False print (A( 'string' ).__bool__())   # True   a  =  A( '' ) print (bool(a))   # False a  =  A( 'string' ) print (bool(a))   # True     # if no __bool__(), then __len__() will be used.(fall back) class  B:      def  __init__( self , a):          self .a  =  a        def  __len__( self ):          return   len ( self .a)     a  =  B( '' ) print (bool(a))   # False a  =  B( 'string' ) print (bool(a))   # True     # if defines neither __bool__() nor __

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

파이썬(Phthon): 내장함수 - __len__ 메서드(method) /// 설명 내장함수 len()을 구현하기 위하여 호출되며, 객체의 길이를 반환합니다.(length >= 0) 참고: __bool__() ※ 형식 object.__len__(self) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class  A:      def  __init__( self , a):          self .a  =  a        def  __len__( self ):          return   len ( self .a)     print (A( 'string' ).__len__())   # 6   a  =  A( 'string' )   print ( len (a))   # 6   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

내장함수 - hash 함수(function) /// 설명 해시값을 반환하여줍니다.(해시값은 정수입니다.) - 같은 입력값에 매번 해시값이 변하는 의문점 ※ 형식 hash(object) 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 49 50 51 52 print (hash( True ))   # 1 print (hash(...))   # -9223363241802529904 print (hash(NotImplemented))   # -9223363241802530356 print (hash( 1 ))   # 1 print (hash( 1. 1 ))   # 230584300921369601 print (hash(complex( 1 ,  1 )))   # 1000004   # ------------------------------------------- print (hash( 'string' ))   #  every time, changed   test_tuple  =  ( 1 ,  2 ,  3 ,  4 ,  5 ) print (hash(test_tuple))   #  every time, changed   test_set  =  { 'a' ,  'b' ,  'c' ,  'd' ,  'e' } test_frozenset  =  frozenset(test_set) print (hash(test_frozenset))   #  every time, changed   class  A:      def  __init__( self , a):        

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

내장함수 - sum 함수(function) /// 설명 반복 가능한 객체(iterable)의 요소들을 전부 합하여 줍니다.(인자 start 가 있을 경우 start까지 합하여 줍니다.) 참고: join(), math.fsum(), itertools.chain() ※ 형식 sum(iterable, /, start=0) 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 test_list  =  [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,  10 ]   test_sum  =  sum(test_list) print (test_sum)   # 55   # [a, b, c, ... d] # a + b + c + ... + d   test_sum  =  sum(test_list,  4 ) print (test_sum)   # 59   # [a, b, c, ... d], start # start + a + b + c + ... + d   test_list  =  [ 1. 1 ,  2. 1 ,  3. 1 ,  4. 1 ,  5. 1 ]   test_sum  =  sum(test_list) print (test_sum)   # 15.5   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __ge__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self >= other: True) other 은 비교할 객체입니다. 참고: __lt__() , __le__() , __eq__() , __ne__() , __gt__() ※ 형식 object.__ge__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __ge__( self , other):          return   self .a  > =  other.a     print (A( 1 ).__ge__(A( 2 )))   # False   a  =  A( 1 ) b  =  A( 2 )   print (a  > =  b)   # False   if  a  > =  b:      print ( 'a is greater than or equal to b' ) else :      print ( 'a is less than b' ) # a is less than b   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __gt__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self > other: True) other 은 비교할 객체입니다. 참고: __lt__() , __le__() , __eq__() , __ne__() , __ge__() ※ 형식 object.__gt__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __gt__( self , other):          return   self .a  >  other.a     print (A( 1 ).__gt__(A( 2 )))   # False   a  =  A( 1 ) b  =  A( 2 )   print (a  >  b)   # False   if  a  >  b:      print ( 'a is greater than b' ) else :      print ( 'b is greater than a' ) # b is greater than a   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __ne__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self != other: True) other 은 비교할 객체입니다. 참고: __lt__() , __le__() , __eq__() , __gt__() , __ge__() ※ 형식 object.__ne__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __ne__( self , other):          return   self .a  ! =  other.a     print (A( 1 ).__ne__(A( 2 )))   # True   a  =  A( 1 ) b  =  A( 2 )   print (a  ! =  b)   # True   if  a  ! =  b:      print ( 'a is not equal to b' ) else :      print ( 'a is equal to b' ) # a is not equal to b   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __eq__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self == other: True) other 은 비교할 객체입니다. 참고: __lt__() , __le__() , __ne__() , __gt__() , __ge__() ※ 형식 object.__eq__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __eq__( self , other):          return   self .a  = =  other.a     print (A( 1 ).__eq__(A( 2 )))   # False   a  =  A( 1 ) b  =  A( 2 )   print (a  = =  b)   # False   if  a  = =  b:      print ( 'a is equal to b' ) else :      print ( 'a is not equal to b' ) # a is not equal to b   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __le__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self <= other: True) other 은 비교할 객체입니다. 참고: __lt__() , __eq__() , __ne__() , __gt__() , __ge__() ※ 형식 object.__le__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __le__( self , other):          return   self .a  < =  other.a     print (A( 1 ).__le__(A( 2 )))   # True   a  =  A( 1 ) b  =  A( 2 )   print (a  < =  b)   # True   if  a  < =  b:      print ( 'a is less than or equal to b' ) else :      print ( 'a is greater than b' ) # a is less than or equal to b   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

파이썬(Phthon): 내장함수 - __lt__ 메서드(method) /// 설명 self 와 other 를 비교합니다.(self < other: True) other 은 비교할 객체입니다. 참고: __le__() , __eq__() , __ne__() , __gt__() , __ge__() ※ 형식 object.__lt__(self, other) reference https://docs.python.org/3/reference/datamodel.html /// 예제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  A:      def  __init__( self , a):          self .a  =  a        def  __lt__( self , other):          return   self .a  <  other.a     print (A( 1 ).__lt__(A( 2 )))   # True   a  =  A( 1 ) b  =  A( 2 )   print (a  <  b)   # True   if  a  <  b:      print ( 'a is less than b' ) else :      print ( 'b is less than a' ) # a is less than b   Colored by Color Scripter cs * 실행환경: Microsoft Windows 10 Homes * 인터프리터: 파이썬(Python 3.9) – 당신을 응원합니다. –

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

내장함수 - oct 함수(function) /// 설명 정수를 접두사 '0o'를 포함하는 8진수 형태로 변환하여 반환합니다. ※ 형식 oct() 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 test_int  =   63 test_hex  =  oct(test_int) print (test_hex)   # 0o77   test_int  =   0b111111 test_hex  =  oct(test_int) print (test_hex)   # 0o77   test_hex  =   '%#o'    # 0x print (test_hex % test_int)   # 0o77 test_hex  =   '%o'    # lowercase print (test_hex % test_int)   # 77   test_hex  =   '{:#o}'    # 0x print (test_hex. format (test_int))   # 0o77 test_hex  =   '{:o}'    # lowercase print (test_hex. format (test_int))   # 77   print (f '{test_int:#o}' )   # 0o77 print (f '{test_int:o}' )   # 77   Colored by Color Scripter cs /// 예제 __index__() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class  A:      def  __init__( self , a):          self .a  =  a        def  __index__( self ):          return   int ( sel