Hướng dẫn python use variable in object name - python sử dụng biến trong tên đối tượng

Thay vì sử dụng một biến mới cho mỗi khách hàng, bạn có thể lưu trữ đối tượng của mình trong từ điển Python:

d = dict()

for record in result:
    objectname = 'Customer' + str(record[0])
    customername = str(record[1])
    d[objectname] = Customer(customername)

print d

Một ví dụ về các đối tượng được lưu trữ trong từ điển

Tôi chỉ có thể giúp bản thân của mình viết một số mã (nhiều hơn những gì tôi đặt ra). Nó giống như gây nghiện. Dù sao, tôi sẽ không sử dụng các đối tượng cho loại công việc này. Tôi có thể sẽ sử dụng cơ sở dữ liệu SQLite (có thể được lưu trong bộ nhớ nếu bạn muốn). Nhưng đoạn mã này cho bạn thấy (hy vọng) làm thế nào bạn có thể sử dụng từ điển để lưu các đối tượng với dữ liệu khách hàng trong:

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()

Trong Python, biến là tên được đặt cho một giá trị, do đó trở nên dễ dàng để đề cập đến một giá trị sau này. Nói cách khác, một biến chỉ ra một đối tượng. Một giá trị theo nghĩa đen được gán cho một biến bằng toán tử

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
3 trong đó phía bên trái phải là tên của một biến và phía bên phải phải là một giá trị. Các phần sau gán một tên cho một giá trị số nguyên.

Bây giờ, bạn có thể tham khảo 10 bằng cách sử dụng một tên số Biến, như hình dưới đây.

Sử dụng chức năng in tích hợp () để hiển thị giá trị của một biến trên replin.

>>> print(num) #display value
10
>>> print(num * 2) # multiply and display result
20

Các biến trong Python là đối tượng. Sử dụng hàm loại () để lấy tên lớp của một đối tượng. Ví dụ: sau đây hiển thị tên lớp của biến

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
4.

>>> type(num)

Loại

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
4 là int. Một đối tượng của lớp INT chứa một số nguyên theo nghĩa đen
# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
6.

Tất cả các biến thực sự là một đối tượng của một lớp tùy thuộc vào giá trị.

>>> greet='Hello World'
>>> type(greet)

>>> isPythonGood = True
>>> type(isPythonGood)

Không giống như các ngôn ngữ lập trình khác như C# hoặc Java, Python là một ngôn ngữ được gõ động, điều đó có nghĩa là bạn không cần phải khai báo một loại biến. Loại sẽ được gán động dựa trên giá trị được gán.

>>> x=100
>>> type(x)


>>> x='Hello World'
>>> type(a)

Các hoạt động khác nhau có thể được thực hiện trên các biến bằng cách sử dụng các toán tử khác nhau dựa trên loại biến. Ví dụ: toán tử

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
7 tổng hợp hai biến INT, trong khi nó kết hợp hai biến loại chuỗi, như được hiển thị bên dưới.

>>> x=5
>>> y=5
>>> x+y
10
>>> x='Hello '
>>> y='World'
>>> x+y
'Hello World'

Danh tính của đối tượng

Mỗi đối tượng trong Python có ID. Đây là địa chỉ của đối tượng trong bộ nhớ được biểu thị bằng giá trị số nguyên. Hàm

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
8 trả về ID của đối tượng được chỉ định nơi được lưu trữ, như được hiển thị bên dưới.

>>> x=100
>>> id(x)
8791062077568
>>> greet='Hello'
>>> id(greet)

ID sẽ được thay đổi nếu một biến thay đổi thành giá trị khác nhau.

>>> x=100
>>> id(x)
879106207
>>> x='Hello'
>>> id(x)
2354658

Nhiều biến được gán cho cùng một giá trị theo nghĩa đen sẽ có cùng một ID, ví dụ:

>>> x=100
>>> id(x)
879106207
>>> y=x
>>> id(y)
879106207
>>> z=100
>>> id(z)
879106207

Do đó, Python tối ưu hóa việc sử dụng bộ nhớ bằng cách không tạo các đối tượng riêng biệt nếu chúng trỏ đến cùng một giá trị.

Nhiều biến gán

Bạn có thể khai báo nhiều biến và gán giá trị cho từng biến trong một câu lệnh, như được hiển thị bên dưới.

Trong ví dụ trên, giá trị INT đầu tiên

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
6 sẽ được gán cho biến X thứ nhất, giá trị thứ hai cho biến thứ hai y và giá trị thứ ba cho biến thứ ba z. Việc gán các giá trị cho các biến phải theo cùng một thứ tự trong chúng được khai báo.

Bạn cũng có thể khai báo các loại giá trị khác nhau cho các biến trong một câu lệnh, như được hiển thị bên dưới.

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
0

Gán một giá trị cho mỗi biến riêng lẻ được phân tách bằng dấu phẩy sẽ ném lỗi cú pháp, như được hiển thị bên dưới.

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
1

Các loại biến phụ thuộc vào các loại giá trị được gán.

# Initiate customer dictionary
customers = dict()

class Customer:
    def __init__(self, fname, lname):
        self.fname = fname
        self.lname = lname
        self.address = None
        self.zip = None
        self.state = None
        self.city = None
        self.phone = None

    def add_address(self, address, zp, state, city):
        self.address = address
        self.zip = zp
        self.state = state
        self.city = city

    def add_phone(self, number):
        self.phone = number


# Observe that these functions are not belonging to the class.    
def _print_layout(object):
        print object.fname, object.lname
        print '==========================='
        print 'ADDRESS:'
        print object.address
        print object.zip
        print object.state
        print object.city
        print '\nPHONE:'
        print object.phone
        print '\n'

def print_customer(customer_name):
    _print_layout(customers[customer_name])

def print_customers():
    for customer_name in customers.iterkeys():
        _print_layout(customers[customer_name])

if __name__ == '__main__':
    # Add some customers to dictionary:
    customers['Steve'] = Customer('Steve', 'Jobs')
    customers['Niclas'] = Customer('Niclas', 'Nilsson')
    # Add some more data
    customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
    customers['Steve'].add_phone('123-543 234')

    # Search one customer and print him
    print 'Here are one customer searched:'
    print 'ooooooooooooooooooooooooooooooo'
    print_customer('Niclas')

    # Print all the customers nicely
    print '\n\nHere are all customers'
    print 'oooooooooooooooooooooo'
    print_customers()
2

Đặt tên quy ước

Bất kỳ định danh phù hợp nào cũng có thể được sử dụng như một tên của một biến, dựa trên các quy tắc sau:

  1. Tên của biến phải bắt đầu bằng chữ cái bảng chữ cái (trường hợp dưới hoặc trên) hoặc dấu gạch dưới (_), nhưng nó không thể bắt đầu bằng một chữ số.
  2. Nhiều hơn một ký tự hoặc dấu gạch dưới alpha có thể theo sau.
  3. Tên biến có thể bao gồm (các) chữ cái, số (các) số và chỉ dấu gạch dưới. Ví dụ:
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    0,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    1,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    2,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    3 là các tên biến hợp lệ, nhưng
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    4,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    5,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    6 là tên biến không hợp lệ.
  4. Tên biến trong Python là trường hợp nhạy cảm. Vì vậy,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    7,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    8,
    >>> print(num) #display value
    10
    >>> print(num * 2) # multiply and display result
    20
    
    9 và
    >>> type(num)
    
    
    0 được coi là tên biến khác nhau.
  5. Tên biến không thể là một từ khóa dành riêng trong Python.

Chúng ta có thể sử dụng tên của một biến trong Python không?

Một tên biến phải bắt đầu bằng một chữ cái hoặc ký tự dấu gạch dưới. Một tên biến không thể bắt đầu với một số. Một tên biến chỉ có thể chứa các ký tự alpha-numeric và nhấn mạnh (A-Z, 0-9 và _). A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

Có phải biến là một đối tượng trong Python?

Biến Python là một tên biểu tượng là một tham chiếu hoặc con trỏ đến một đối tượng.Khi một đối tượng được gán cho một biến, bạn có thể tham khảo đối tượng bằng tên đó.Nhưng chính dữ liệu vẫn còn trong đối tượng.. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object.

Có phải mọi biến python chỉ vào một đối tượng?

Đột biến thay đổi đối tượng, không phải biến.Nhưng các biến trỏ đến các đối tượng.Vì vậy, nếu một biến khác trỏ đến một đối tượng mà chúng ta vừa đột biến, thì biến khác sẽ phản ánh cùng một thay đổi;Không phải vì biến đã thay đổi mà bởi vì đối tượng nó chỉ ra thay đổi.variables point to objects. So if another variable points to an object that we've just mutated, that other variable will reflect the same change; not because the variable changed but because the object it points to changed.

Tôi có thể sử dụng ID làm tên biến trong Python không?

ID có thể được sử dụng như một tên biến trong Python không?ID () là một hàm trong Python, vì vậy bạn nên không sử dụng một biến có tên ID.Ghi nhớ điều đó, điều đó áp dụng cho tất cả các chức năng mà bạn có thể sử dụng một biến không nên có cùng tên với một hàm.it's recommend not to use a variable named id. Bearing that in mind, that applies to all functions that you might use a variable shouldn't have the same name as a function.