티스토리 뷰

Python

Python, @property

hwangyoungjae 2016. 5. 9. 11:04
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

>> @property <<

메서드를 변수처럼 사용할수 있게 함

-. @property를 입력하지 않았을때

class myclass:

    def __init__(self,n):

        self.name=n

   

    def p_test(self):

        print(self.name.upper())

 

test=myclass('abcd')

test.p_test() #일반적인 함수처럼 사용

 

실행결과

ABCD

 

-. @property를 입력했을때

class myclass:

    def __init__(self,n):

        self.name=n

 

    @property

    def p_test(self):

        print(self.name.upper())

 

test=myclass('abcd')

test.p_test #변수처럼 사용가능해짐

 

실행결과

ABCD

 

위에서 볼수 있듯이 입력받은 문자열을 대문자로 반환하는 함수를 생성후 호출하면 위의 함수는 property를 입력하면 변수처럼 괄호를 붙이지 않아도 해당 해당 메소드호출이 가능해진다.

 

'Python' 카테고리의 다른 글

Python, 클래스(Class)  (0) 2016.05.09
Python, 정적메소드,클래스메소드  (0) 2016.05.09
Python,module copy [객체복사]  (0) 2016.04.29
Python,module re [정규표현식]  (0) 2016.04.29
Python, 예외처리  (0) 2016.04.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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
글 보관함