Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to call Class method "func" that supposed to print something but it never gets printed...No errors but just silence. Code bellow:
class AnyClass():
atr1=0
atr2='text'
def func():
print ('Ran Func')
a = AnyClass()
a.func
a.func is just the name of a method object. You have to call it with ().
>>> a.func
<bound method AnyClass.func of <__main__.AnyClass object at 0x0000000003506240>>
>>> a.func()
Ran Func
Also note that you have to pass self to the method as the first argument when defining it unless the #staticmethod decorator is used.
def func(self):
print ('Ran Func')
a.func is just a reference to the function object. To call it, you need to add parenthesis.
a.func()
function should have parenthesis like a.func()
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't have any reason why I'd want to do this, but I was still wondering, why do these throw syntax errors:
def f():
return print # syntaxError: invalid syntax
def f():
return print() # syntaxError: invalid syntax
But returning other built-in functions is completely ok, like for example these:
def f():
return map # no errors
def f():
return len() # no errors
Also I can print any built-in function:
>>> print(map)
<built-in function map>
>>> print(set)
<type 'set'>
But I cannot print the print function:
>>> print(print("test"))
File "<stdin>", line 1
print(print("test"))
^
SyntaxError: invalid syntax
You tagged the question python-3.x, but you seem to be running with Python 2.
print is a keyword in Python 2.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to update a clients details with a function. But it doesn't work. I debug the program and I realized that it never goes into the if. I don't know why?
This is the function:
def UpdateClient(self,id,cnp,name,adress):
for i in range (len(self.clients.get_all())):
if self.clients.get_all()[i].get_id==id:
self.clients.get_all()[i].set_name(name)
self.clients.get_all()[i].set_cnp(cnp)
self.clients.get_all()[i].set_adress(adress)
When I try to update the client, I give the id, name, cnp, and adress, but when I print the list nothing is changed. I cannot go into the if with the debugger because it said that they are never equal. Why?
Note that get_id is not the same as get_id(). The former references the method, while the latter actually calls it.
In addition, consider simplifying your code by writing more idiomatic Python. For example:
for client in self.clients.get_all():
if client.get_id() == id:
client.set_name(name)
...
The issue here is you are making your check as get_id == id but it is a function. Your if should be like:
if self.clients.get_all()[i].get_id()==id:
# ^ making it a function call
Also, better way to do this is by storing self.clients.get_all() in a separate variable. Hence, your code should look like:
def UpdateClient(self,id,cnp,name,adress):
clients = self.clients.get_all()
for i in range (len(clients)):
if clients[i].get_id()==id:
clients[i].set_name(name)
clients[i].set_cnp(cnp)
clients[i].set_adress(adress)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to add 'dir' method to my object so when dir() is called on it, my function will be called.
My code looks like this:
class c(object):
def __dir__(self):
return ["a"]
print dir(c())
print c().__dir__()
Only the second print shows ["a"], and the first acts like normal.
I've tried this in several ways and sometimes it works and sometimes it doesn't. Any ideas why?
Edit:
I was missleading, my code is more complex then I showed. To be exact, my code creates an object 'x' with dir method and writing:
x.__dir__()
Works, but
x.__dir__() == dir(x)
Returns false
I suspect you are writing dir(c) instead of dir(c()) somewhere. The difference is calling dir on a class (dir(c)) invokes the class object which itself has a default __dir__(), which will give the undesired default behavior. What you have in your question right now (dir(c())) should work.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a function call with a dictionary like:
valueattime(simulaton_01['Temperature_05'], 20)
but I always get a syntax error with the following function
def valueattime(experiment[key], time):
...
It works with simple parameters. But for autocompletion sake it would be great to pass the parameters in the dictionary-form.
Any ideas?
You don't need to change the function signature to pass parameters directly from the dictionary:
def valueattime(temperature, time):
...
valueattime(simulation_01['temp_05'], 20) # this works just fine
Python will first run simulation_01['temp_05'] to retrieve the value, and pass it as temperature to the function.
You should pass it as a regular argument:
def valueattime(temp, time):
pass
a = {'some_key': 'some_value'}
valueattime(a['some_key], 20)
This works!
To supply dictionary items to a function, you can use dictionary comprehension:
new_dict = {k: valueattime(v) for k, v in a.iteritems()}
Remember it'a all objects...
def valueattime(temp, time)
Defines a method taking two input parameters, two object which are referred to by the names temp and time
simulation_01['temp_05'] return an object, so calling your method like: valueattime( simulation_01['temp_05'], 20 ) should be what you are looking for
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#!/usr/bin/python
def maker(n):
def action(x):
return x*n
return action
f=maker(2)
print f(3)
I have problem here with type error.How to solve this two arguments problem,one is fixed.
You need to put the return action out of action function :
def maker(n):
def action(x):
return x*n
return action
f=maker(2)
print f(3)
Result:
6
Note that in factory function you must return the inner function as the result of main function.