0

I'm building my 1st Python app, a car service where clients can request vehicles for transportation.

I have a "Clients.txt" file (in the same folder) with the Usernames and Passwords of each client, and I've successfully created the function "read_clients()" that opens the said file and stores each username in "client_username" and each password in "client_password".

What I want to do now is create an object in the Class "Client" for every client, and automatically make each object's self.username = client_username and each object's self.password = client_password.

I tried using a for loop. I think I shouldn't call each object c1, as I think this would just keep overwriting the c1 variable, right? But what's the solution? Should I implement some "counter" variable and increase it by 1 with each cycle and then create the variable c"counter"?

Should I use a dictionary for this instead? Maybe not, because I have a series of functions like create_account() and login() which I'll want to run on each object's variables. Right?

Thanks!

I tried defining the read_clients function both inside and outside of the "Clients" class and removing and applying the "self" attribute.

class Client:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def read_clients(self):
        with open("Clients.txt", "r") as file:
            client_list = file.readlines()
            for pre_client in client_list:
                client = pre_client.split(" ")
                client_username = client[0]
                pre_password = client[1]
                client_password = pre_password.strip("\n")
                c1 = Client(client_username, client_password)

2 Answers2

1

Store your newly created clients in a list and return such list as result. Given you are not using self inside read_clients, you can define it as static using @staticmethod

Code

class Client:
    def __init__(self, username, password):
        self.username = username
        self.password = password

    @staticmethod
    def read_clients():
        with open("Clients.txt", "r") as file:
            client_list = file.readlines()

            # Store your clients here
            clients = []

            for pre_client in client_list:
                client = pre_client.split(" ")
                client_username = client[0]
                pre_password = client[1]
                client_password = pre_password.strip("\n")

                # Save each new client
                clients.append(Client(client_username, client_password))

            # Return the created clients
            return clients

clients = Client.read_clients()
JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
  • @AntónioGonçalves did my answer work for you? If so, please, mark it as **accepted** (green checkmark), otherwise, let me know what went wrong. Thnx in advance! – JoshuaCS Jan 14 '19 at 13:51
0

If I understand correctly, I suggest you to use a list. Example code (write it ourside the class):

 def read_clients():
        retuned_list=[]
        with open("Clients.txt", "r") as file:
            client_list = file.readlines()
            for pre_client in client_list:
                client = pre_client.split(" ")
                client_username = client[0]
                pre_password = client[1]
                client_password = pre_password.strip("\n")
                client = Client(client_username, client_password)
                returned_list.append(client)
        return returned_list