How do I tell Python an object is a list?

时间:2018-03-25 18:55:49

标签: python function object data-structures

I felt real close finalizing a function I had been working on for some time, but there was a problem. I can't seem to be able to communicate with Python that an object I'm passing through a function is a list object.

This is my function: log(a, b, c)

My log function is supposed to take a string 'b', and append it to a list 'a' that may or may not exist already, and then take the -1 index of list 'a' (which is the b string) and append it to list 'c'.

I needed mostly more automation out of this function. At the moment, without this function, on a daily basis I have to always code and define 'a' first, then add 'b' to it, append a bunch of stuff, and add a['b'] to c! Way too much work! I need the function to automate that entire process. This is the code I used in my attempt to implement my function:

def log(a, b, c):
    try:a
    except NameError: a= None
    if a is None:
                a=list()
                a.append(b)
                c.append(a[-1])

    else:
                a.append(b)
                c.append(a[-1])

When I try to use the function, in a case where 'a' is non-existent, Python keeps telling me:

NameError: Name 'a' is not defined

And that is without quotation marks. If I put quotation marks around the 'a' object, it then tells me:

AttributeError: 'str' object has no attribute 'append'

basically telling me that Python doesn't understand that the 'a' object is supposed to be a list, not a string (why is talking to computers so hard?).

My function only works if object 'a' ('c' is not a concern because you can consider those a set of lists that are a constant in the program, 'a' is more of a variable because I need a new list everyday) is defined before the runtime of the log().

How can I pass an object through my function, and have Python accept that if it does not exist, it should be a new list object?

People are asking for more code. Like I said in the comments, this is just the test of a function which will be eventually integrated into an existing script, where any value of ‘c’ exists already, but here’s what happens at the prompt:

>>>log(“March_25_2018”,{“Log_1”:”Test PMIC tonight.”},Project_Logs)

I’m forced to put March_25_2018 in quotation if it has not been defined before the run of the function. This is the problem; Python handles ‘a’ as a string and not a list. ‘c’ is not a concern, because in the final program, c is always defined anyway.

    >>>Tracback (most recent call last):
   File “<pyshell#73>”, line 1, in <module>
     log(“March_25_2018”,{“Log_1”:”Test PMIC tonight.”},”Project_Logs”)
   File “User_Space/.py”, line 10, in log
   a.append(b)
   AttributeError: ‘str’ object has no attribute ‘append’.

2 个答案:

答案 0 :(得分:2)

TL;DR: a is not an object; it might be a reference to an object, if you assign a value to the name first.


It sounds like you are literally calling your function as

log(a, b, c)

without first defining what a, b, and c are. You need to do that, with something like

a = []
b = "foo"
c = []
log(a, b, c)

Note that the names you see don't matter; the following behaves the same:

some_list = []
my_str = "foo"
some_other_list = []
log(some_list, my_str, some_other_list)

The names a, b, and c only exist inside the function, to refer to the values you put in the call.

The function need only be defined as

def log(a,b,c):
    a.append(b)
    c.append(a[-1])

because part of the contract of the function is that the first and third arguments need to be objects that have an append method, and further the first argument has to support __getitem__, for a[-1] to work (although you could avoid that by just writing c.append(b); after the first line you have the invariant b == a[-1]).

There is no way, short of executing del a, for a not to be defined in the body of log, because all named parameters are defined upon entry to the function.

答案 1 :(得分:-3)

This may be because you are trying to use or print the a variable after the function, while a is only a temporary variable inside the function