레이블이 VS인 게시물을 표시합니다. 모든 게시물 표시
레이블이 VS인 게시물을 표시합니다. 모든 게시물 표시

7/07/2015

[Python] 같은 이름을 가진 Parameter(파라미터)와 함수 내의 local variable(지역 변수)

결과적으로 다른 값을 가지면 다르고, 같은 값을 가지면 같은 것을 의미함

id 함수를 통해 이를 테스트 해볼 수 있다.

https://wikidocs.net/32 에서 id 항목을 참고
# id(변수명)은 메모리 위치를 출력, 이로 같은 위치에 있는 변수인지 확인


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def parameter_local_variable_test(a):
 print("init : ", hex(id(a)))
 a = 10
 print("assign 10 to a : ", hex(id(a)))
 a = 5
 print("assign different value : ", hex(id(a)))
 a = 5
 print("same value : " ,hex(id(a)))
 
# >>> test(10)
# init :  0x6f43c0a0
# assign 10 to a : 0x6f43c0a0    # If argument is same with 10, not changed
# assign different value : 0x6f43c050   # changed
# assign same value : 0x6f43c050        # not changed
 
# >>> test(2)
# init :  0x6f43c020
# assign 10 to a : 0x6f43c0a0    # if argument is different with 10, changed
# assign different value : 0x6f43c050   # changed
# assign same value : 0x6f43c050        # not changed


[추가 자료] 파이썬 변수에 대한 이해를 돕는 링크. 그림과 코드만 봐도 쉽게 이해가 됨

11/07/2014

Java - Type Casting vs valueOf

Java의 BufferWriter로 윈도우와 소켓통신을 하려는데 write 함수가 char array 을 데이터로 받는다.

int32나 uint64로 type으로 윈도우에서 받으려고 하는데, 내쪽에서 그냥 char로 casting 해서 보내면 default system encoding 으로 system dependency가 생길거 같아, 어떻게 해야할지 찾아보았다.

Just two different ways of doing the same thing. It may be a historical reason (can't remember if one came before the other).

큰 차이는 없다는 것 같기도 하면서

아래에 보면 Exception throw에서 차이가 있는 듯하다.

One huge difference is that if you invoke toString() in a null object you'll get a NullPointerException whereas, using String.valueOf() you may not check for null.


누구는 Integer가 null object가 없으니 valueOf 에서도 null check을 안한다고 하는데, 바로 아래에서는 된다고 하니 확인이 필요하다.



Ref: http://stackoverflow.com/questions/3335737/integer-tostringint-i-vs-string-valueofint-i




[Java] PrintWriter 와 BufferWriter 차이 그리고 Writer에서 print와 write의 차이

PrintWriter : system.out.print 처럼 쉽게 쓸수 있는게 장점. println 도 제공.

BufferWriter : 효율적으로 메모리를 사용할 수 있음.

두 Writer의 이점을 살릴려면, wrap 하여 객체를 생성한다.

PrintWriter writer = new PrintWriter(
                         new BufferedWriter (
                             new FileWriter("somFile.txt")));

Ref: http://stackoverflow.com/questions/1747040/difference-between-java-io-printwriter-and-java-io-bufferedwriter



Writer 에서

print : writer의 wrap function. 위에서 말한대로 system.out.print 처럼 쉽게 쓸 수 있음.

write: 입력할 길이 조정 가능
  This method cannot be inherited from the Writer class because it must suppress I/O exceptions.


Ref: http://www.coderanch.com/t/174675/java-Web-Component-SCWCD/certification/print-write-PrintWriter

10/03/2014

C - perror vs fprintf stderror

쓰다보니 두 개의 차이를 모르겠다. perror도 stderror에 print하는 건데.

http://stackoverflow.com/questions/12102332/when-i-should-use-perror-and-fprintfstderr


(훑어본) 요약

perror 는 syscall의 errno 를 overwrite하지 않음. system error print 가됨

fprintf stderror 는 errno에 영향을 미침. custom error print.