Quick actions

cmd+k|ctrl+k

Navigation

Languages

20241206Class

Snippet info

Language

Python

Visibility

public

Author

henryting

Created

2024-12-06T01:36:29.137645Z

Updated

2024-12-06T15:36:13.653576Z

# class Toy:
#     head='blue'
#     body='blue'
#     def a():
#         print('class internal function')
# toy1=Toy()
# print(toy1)
# Toy.a()

# class Toy:
#     '''this is a toy class'''
#     head='blue'
#     body='blue'
#     def a(self):
#         print('class internal function')
#     def __init_(self, leg):
#         self.leg=leg
        
# toy1=Toy(2)
# print(Toy.__dict__)
# print(toy1.__dict__)
# print(toy1.a)   #Toy.a
# print(toy1.a()) #Toy.a(toy1)
# toy1.x=10
# print(toy1.__dict__)
# toy2=Toy(4)
# print(toy2.__dict__)


#How python store data?

# #case 1 string look like identifier
# a='hello'
# b='hello'
# print(id(a))
# print(id(b))                         
 

# #case 3 long string but look like identifier
# a='hello_world'
# b='hello_world'
# print(id(a))
# print(id(b))

# #case 4
# a='_this_is_a _long_string_that_could_be_used_as_an_identifier'
# b='_this_is_a _long_string_that_could_be_used_as_an_identifier'
# print(id(a))
# print(id(b))

# #case 5
# a='1_hello_world'
# b='1 hello world'
# print(id(a))
# print(id(b))

# #case 6 not interned
# a='1 hello world'
# b='1 hello world'
# print(id(a))
# print(id(b))

# #case 7 interning strings
# a='this _is _a_long_string'
# b='this _is _a_long_string'
# print('a==b:', a==b)
# print('a is b:', a is b)

# #8 can't use is
# a='hello world'
# b='hello world'
# print('a==b:', a==b)
# print('a is b:', a is b)

# #9 force intern a, b must be set intern
# import sys
# a=sys.intern('hello world')
# b=sys.intern('hello world')
# c='hello world'
# print(id(a))
# print(id(b))
# print(id(c))

# #case 10 compare speed
# def compare_using_equals(n):
#     a=sys.intern('a long string that is not interned' * 200)
#     b=sys.intern('a long string that is not interned' * 200)
#     for i in range(n):
#         if a==b:
#             pass

# def compare_using_interning(n):
#     a=sys.intern('a long string that is not interned' * 200)
#     b=sys.intern('a long string that is not interned' * 200)
#     for i in range(n):
#         if a is b:
#             pass
        
# import time
# start=time.perf_counter()
# compare_using_equals(10000000)
# end=time.perf_counter()

# print('equality: ', end-start)

# start=time.perf_counter()
# compare_using_interning(10000000)
# end=time.perf_counter()
# print('interning: ', end-start)

##############################################
#Star-Argument

# def a(x, *arg):
#     print('Hello World!', x, arg)

# a(1,2,3,4,5,6)


# def a(x,y, *arg):
#     print('Hello World!', x, y, arg)
#     def b(h,i):
#         print(h,i)
#     b(arg[0],arg[1])
# a(1,2,3,4,5,6)

#------------------------------------------------
# def a(x,y=1, *arg):
#     print('Hello World!', x, y, arg)
#     def b(h,i):
#         print(h,i)
#     b(arg[0], arg[1])
# a(1,2,3,4,5,6)


#-------------------------------------------------
# def a(x,y,z):
#     print('a={1}, b={0}, c={2}'. format(x,y,z))

# a(1,2,3)

#--------------------------------------------------
# def a(x,y=2,z=3):
#     print('a={1}, b={0}, c={2}'. format(x,y,z))

# a(1,2,3)


#keyword argument-----------------------------------
# def a(x,z, y=2):
# print('a={1}, b={0}, c={2}'. format(x,y,z))

# a(1,2,3)


#----------------------------------------------------
# def a(x,*arg, z=1, **kwarg):
#     print(x, arg, kwarg)

# a(1,2,3,4,z,y=6)

# ans: 1(2,3.4) {'z':3, 'y':6}


#----------------------------------------------------
# def a(x,*arg, z=4, **kwarg):
#     print(x, z,arg, kwarg)

# a(1,2,3,4,z=3,y=6)

# print(globals())
# #globals for scan all variable and value in function
# print(a.__str__)

#ans:1 3 (2, 3, 4) {'y': 6}


#------------------------------------------------------
def a(x,*arg, z=4, **kwarg):
    print(x, z,arg, kwarg)
file(__name__=='__main__'): #for make sure the location of file while running

a(1,2,3,4,z=3,y=6)
print(globals())
#globals for scan all variable and value in function
print(a.__str__, a.__name__)  #a
INFO