final project - AttributeError: 'function' object has no attribute 'method' - CS50 Stack Exchange
AttributeError: 'function' object has no attribute '__func__'
Don't know how to fix AttributeError: 'function' object has no attribute 'pk' when trying to create new users from registration form
python - How to Prevent "AttributeError: 'function' object has no attribute ''" - Stack Overflow
Videos
You are receiving an error because alphabet is a variable defined within a function. It is therefore a local variable which cannot be accessed outside of that function.
I am very confused as to why there is a function there in the first place. I would replace everything under the alphabet function with just the variable containing the characters in order.
There are actually quite a few things wrong with this code - the code in the for loop is flawed as well. Here is my solution, with the added benefit of printing the encoded message as one string and not as a load of characters, as well as dealing with non-alphanumeric characters.
elif option1 == 2:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
message = input('What message would you like to encrypt?')
encoded = []
for character in message:
if character.isalpha():
encoded.append(alphabet[25-alphabet.find(character)])
else:
encoded.append(character)
print(''.join(encoded))
The alphabet variable is a local variable, so you can only call it in the alphabet() function. When you put alphabet.find(), it thought you alphabet was the function, not the variable.
You can do:
elif(option1 == 2):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in message:
print(alphabet[25-alphabet.find(char)])
That should work.
This similar question helped me discover the problem.
The Python documentation for the inspect module mentions both "frame records" and "frame objects", and explains the difference.
inspect.currentframe()returns a frame object, butinspect.getouterframes()returns a list of frame records.
The mistake in the code above is not extracting the frame object from the frame record of the calling function, and passing inspect.getouterframes() the frame record instead of the frame object. (Note that inspect.getouterframes() doesn't check that its argument is a frame object.)
Here's the fixed definition of caller_args() (with the change to the assignment to caller_frame):
def caller_args():
frame = inspect.currentframe()
outer_frames = inspect.getouterframes(frame)
caller_frame = outer_frames[1][0]
return inspect.getargvalues(caller_frame)
Which runs as desired:
$ python getargvalues_test_fixed.py
ArgInfo(args=['arg1'], varargs=None, keywords=None, locals={'arg1': 'foo'})
cause of error
AttributeError: 'tuple' object has no attribute 'f_code'
in your function
def caller_args()
is that caller_frame is an array of which you need item [1][0] as argument for
inspect.getargvalues(...)
this works :
currframe = inspect.currentframe()
callerframe = inspect.getouterframes(currframe, 2)
inspect.getargvalues(callerframe[1][0])
Also, the getargvalues function returns 4 values. First three are unimportant in this case, fourth contains JSON like format key/value list of callerframe arguments
_,_,_,values = inspect.getargvalues(callerframe[1][0])
for i in values:
argsstring += str(i) + ' : ' + str(values[i])
My test looks like this :
import inspect
def log(text):
currframe = inspect.currentframe()
callerframe = inspect.getouterframes(currframe, 2)
_,_,_,values = inspect.getargvalues(callerframe[1][0])
argsstring = ''
for i in values:
argsstring += str(i) + ' : ' + str(values[i])
print('name of file : ' + callerframe[1][1])
print('name of function : ' + callerframe[1][3])
print('line number : ' + str(callerframe[1][2]))
print('caller function arguments : ' + argsstring)
def doTest(text):
log(text)
doTest('this is a test')
If this line
new_x = np.linspace(x.min(), x.max(), new_length)
is generating the error message
AttributeError: 'function' object has no attribute 'min'
then x is a function, and functions (in general) don't have min attributes, so you can't call some_function.min(). What is x? In your code, you've only defined it as
x=var
I'm not sure what var is. var isn't a default builtin in Python, but if it's a function, then either you've defined it yourself for some reason or you've picked it up from somewhere (say you're using Sage, or you did a star import like from sympy import * or something.)
[Update: since you say you're "using PyLab", probably var is numpy.var which has been imported into scope at startup in IPython. I think you really mean "using IPython in --pylab mode.]
You also define x1 and y1, but then your later code refers to x and y, so it sort of feels like this code is halfway between two functional states.
Now numpy arrays do have a .min() and .max() method, so this:
>>> x = np.array([0.1, 0.3, 0.4, 0.7])
>>> y = np.array([0.2, 0.5, 0.6, 0.9])
>>> new_length = 25
>>> new_x = np.linspace(x.min(), x.max(), new_length)
>>> new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
would work. Your test data won't because the interpolation needs at least 4 points, and you'd get
ValueError: x and y arrays must have at least 4 entries
Second question: how do I input more than one line of code at once? At the moment, if I try to copy the whole thing and then paste it into PyLab, it only inputs the top line of my code, so I have to paste the whole thing in one line at a time. How do I get round this?
Assuming you're in ipython called as ipython --pylab or something similar, then you can simply use the paste magic command. Call it as %paste or simply paste if you haven't defined paste as another variable:
In [8]: paste
import numpy as np
import scipy as sp
from scipy.interpolate import interp1d
x=var
x1 = ([0.1,0.3,0.4])
y1 = [0.2,0.5,0.6]
new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
## -- End pasted text --
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-b4e41f59d719> in <module>()
3 from scipy.interpolate import interp1d
4
----> 5 x=var
6 x1 = ([0.1,0.3,0.4])
7 y1 = [0.2,0.5,0.6]
NameError: name 'var' is not defined
In [9]:
morseCodeM={".-":"A",
"-...":"B",
"-.-.":"C",
"-..":"D",
".":"E",
"..-.":"F",
"--.":"G",
"....":"H",
"..":"I",
".---":"J",
"-.-":"K",
".-..":"L",
"--":"M",
"-.":"N",
"---":"O",
".--.":"P",
"--.-":"Q",
".-.":"R",
"...":"S",
"-":"T",
"..-":"U",
"...-":"V",
".--":"W",
"-..-":"x",
"-.--":"Y",
"--..":"Z",
" / ":" "}
def morseToText(inp):
out=""
while(inp!=" "):
for i in morseCodeM:
if(inp.find(i)!=-1): <-----------------------
out=out+morseCodeM[i]
inp2=list(inp)
inp2[inp.find(i):inp.find(i)+(len(i)-1)]=""
inp="".join(inp2)
return outI honestly don't know whats wrong it just gives the error :"AttributeError: 'function' object has no attribute 'find'" on the line with the arrow .
edit: the code is about converting morse code to text