oop issue

Avi Gross avigross at verizon.net
Mon May 23 19:57:41 EDT 2022


invest_crypto.client_list.append(self)

I am wondering about the phrasing above.

When you are in the dunder init function, you normally create and change items in YOURSELF so why is your code not changing self.crypto_client_list?

And what are you appending to before ever creating it? Would it kill you to create some form of container in the main class definition initialized appropriately to some variation of empty?

I won't claim to fully understand what the code wants to do. Adding yourself to the list of clients may make sense to you but if you understand object oriented programming, you may have an inkling that objects need to be CREATED somewhere before they can be used. Most of your code looks like it is DEFINING an object. The last few lines try to access a method in an object that has never been instantiated. Yes, you do have a way to store methods in a class and call them without any objects but this is not a case like that.

You need something like "myobj = invest_crypto(args)" and then the rest of your code can do changes and calculations and perhaps create other similar or different objects. You seem to be using a method that reads in a file and dumps a version of the contents and that might work if you did a line like this next: "myobj.access_client_details()" albeit not how I would name it or do it.

And, of course, your print() again names the class, not an instance of a class. 

Your code wraps in a few places and I wonder if it contains errors as in this set of lines:
        return f"('{self.name}', '{self.surname}', '{self.amount_Deposited}',
        '{self.amount_to_transfer}')"

Is that really how you think you set up a formatted string? 

Even if you get that working, how does the print statement know what to do with a list of objects?

Some might design a method you can call to print the contents of a list which would loop over the list. But is there ever more than one client (yourself) in the list?

The above rambling is just reflecting my opinion that you have not learned enough or thought it through and may even be copying and modifying various snippets of code perhaps from places where it works to a place that might be better designed from scratch.

Have fun. As someone else mentioned, smaller more focused examples may work better to get you up to speed, but then again, we often find out someone is given a homework assignment ...




-----Original Message-----
From: Tola Oj <ojomooluwatolami675 at gmail.com>
To: python-list at python.org
Sent: Mon, May 23, 2022 4:54 pm
Subject: oop issue

i just finished learning oop as a beginner and trying to practice with it
but i ran into this typeerror issue, help please.

Traceback (most recent call last):
  File
"c:\Users\ojomo\OneDrive\Desktop\myexcel\oop_learn.py\myExperiment.py\mainMain.py",
line 36, in <module>
    print(invest_crypto.client_list)
TypeError: invest_crypto.__repr__() missing 1 required positional argument:
'self'

this is my code below:
import csv
class invest_crypto:
    crypto_current_rate = 0.05
    client_list = []
    def __init__(self, name, surname, amount_Deposited, amount_to_transfer):
        self.name = name
        self.surname = surname
        self.amount_Deposited = amount_Deposited
        self.amount_to_transfer = amount_to_transfer

        invest_crypto.client_list.append(self)

    def calculate_customer_transfer(self):
        self.customer_transfer = (self.crypto_current_rate * self.
amount_Deposited) + self.amount_Deposited
        return self.customer_transfer

    @classmethod
    def access_client_details(cls):
        with open('C:\\Users\\ojomo\\OneDrive\\Desktop\\myexcel\\
oop_learn.py\\myExperiment.py\\clientDetails.csv', 'r' ) as f:
            reader = csv.DictReader(f)
            clientDetails = list(reader)

            for item in clientDetails:
                invest_crypto(
                    name=item.get('name'),
                    surname=item.get('surname'),
                    amount_Deposited=item.get('amount_deposited'),
                    amount_to_transfer=item.get('amount_to_transfer')
            )
    @staticmethod
    def __repr__(self):
        return f"('{self.name}', '{self.surname}', '{self.amount_Deposited}',
'{self.amount_to_transfer}')"


invest_crypto.access_client_details()
print(invest_crypto.client_list)
-- 
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list