I want to use the class for the values in the list (add code and error message)

Asked 1 years ago, Updated 1 years ago, 81 views

a = class()

b = class()

c = class()

.

.

z = closs()

In order to use the class, you must enter "Instance = Class()". In this way, I didn't type in from a to z one by one, but I made a list and made a repeat sentence. And to see how many instances were created, we created a function in the class method to check the count.

my_list = [ 'a', 'b', 'c' .... 'z' ]

The count was identified as 26 alphabets. I checked the type by entering type(a). An error message was received saying that it could not be found.

If you type a = class() directly, an instance is created, and the class is correct when you check the type When I used the list and repeat statements, I got an error message that the instance was created as long as the list, but I couldn't find it when I checked the type.

I wonder why these results come out.

Add code and error message to question July 29.


class Test_class:
    count = 0

    def __init__(self):
        Test_class.count += 1

    @classmethod
    def print_count(cls):
        print ('{0}'.format(cls.count))


Test_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']


def test_def():

    list2 = []
    index0 = 0

    for i in range(len(Test_list)):
        n = index0
        target = Test_list[index0]
        list2.append(target)
        k = Test_list[index0]
        k = Test_class()
        index0 = index0 + 1

    print(list2)


test_def()

Test_class.print_count()

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
There are nine.

Running time: 13.80 ms

I can confirm that it has progressed well so far.

If you then enter type(a), the following error message appears:

type(a)

An error occurred while running.
Traceback (most recent call last):
File "/solution.py", line 36, in <module>
type(a)
NameError: name 'a' is not defined

python list index class

2022-09-21 16:15

1 Answers

The variable a and the string "a" are completely different.

a = 'b'
b = 'a'

It's possible. OK?

If you want to put 26 Test_class instances in Test_list,

Test_list = [ Test_class() for i in range(26) ]

This is how you do it. To put each of these instances in a variable up to a, ..., z (why is it intractable a, b, c...)I don't know if you want to save it under the name )

a = Test_list[0]
b = Test_list[1]
# ... 
z = Test_list[25]

I'll do it like this.

Let's answer the additional comments.

I'll explain why the code you posted doesn't work as intended.

To put a Test_class instance in the Test_list in the code:

Test_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

#... For inside the door
k = Test_list[index0] # Take one of the Test_list out. Let's say k = 'c'.
k = Test_class() # Create a Test_class instance and assign it to k. The questioner seems to think that this is the same as c = Test_class().

In the above code, the k = Test_class() statement does not replace a = Test_class() and b = Test_class(). Suppose k becomes "c" in the sentence just above. Then, will the k = Test_class() statement be c = Test_class()? As I said at the beginning of the first answer, the variable c and the string "c" are completely different. I think you should understand this first.

If you understand this, you will understand the following errors.

When you do type(a), the error occurs that the name name 'a' is not defined and the name a are not defined because the a variable has never been created and written before.

If you want to create and manage Person instances with names such as maria and james, there is no point in specifying maria, james, etc. as variable names. If the names of maria, james, etc. are important data, you can create a member variable of the Person class to store the names, and store the names there. The variable name doesn't matter if it's p, q, r, people[0], people[1], ...

OK?


2022-09-21 16:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.