This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass(object): | |
| data = 0 | |
| def __init__(self, value): | |
| self.instance_data = value | |
| MyClass.data | |
| # 0 | |
| my_instance = MyClass(3) | |
| my_instance.data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass(object): | |
| data = [] | |
| def __init__(self, value): | |
| self.instance_data = value | |
| my_instance = MyClass(3) | |
| my_instance.data | |
| # [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try: | |
| f = open('test_file.txt', 'r') | |
| # do some operations | |
| finally: | |
| if f: | |
| f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try: | |
| f = open('test_file.txt', 'r') | |
| print(f.read()) | |
| finally: | |
| if f: | |
| f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with open('test_file.txt', 'r') as f: | |
| print(f.read()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bisect | |
| with open('ip1.txt', 'r') as f1: | |
| list1 = [line.rstrip('\n') for line in f1.readlines()] | |
| with open('ip2.txt', 'r') as f2: | |
| list2 = [line.rstrip('\n') for line in f2.readlines()] | |
| list2.sort() | |
| same_ip = [] | |
| for i in list1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with open('test.txt', 'w') as f: | |
| f.write('Hello, world!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Example 1 | |
| with open('test1.txt', 'w') as f: | |
| f.writelines(["1", "2", "3"]) | |
| # The contents of test1.txt is: | |
| # 123 | |
| # Example 2 | |
| with open('test2.txt', 'w') as f: | |
| f.writelines(["1\n", "2\n", "3\n"]) | |
| # The contents of test2.txt is: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The contents in test.txt are: | |
| # Hello,Yang! | |
| with open('test.txt', 'r+') as f: | |
| print(f.read()) | |
| f.seek(6, 0) | |
| print(f.read()) | |
| # Hello,Yang! | |
| # Yang! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def isValid(self, s: str) -> bool: | |
| mapping = {')': '(', ']': '[', '}': '{'} | |
| stack = [] | |
| for char in s: | |
| if mapping.get(char): | |
| if not stack: | |
| return False | |
| else: | |
| if stack[-1] == mapping[char]: |