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.
Related
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 10 months ago.
The community reviewed whether to reopen this question 10 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to find out whether two enum instances represent the same item of an enum class. Imagine the following simple program which works as expected:
from enum import Enum, auto
class myEnum(Enum):
ITEM1 = auto()
ITEM2 = auto()
enum1 = myEnum.ITEM1
enum2 = myEnum.ITEM2
enum3 = myEnum.ITEM1
print(enum1 == enum2)
print(enum1 == enum3)
print(enum1 == myEnum.ITEM1)
print(enum1 == myEnum.ITEM2)
Output:
False
True
True
False
My real application is slightly more complicated with creation of enum instances at different places, but all of the same class defined in a single module. Here the result is not as expected, even if the instances reflect the same value:
itype
<InstrumentTypes.FIXEDPOINTCELL: 5>
type(itype)
<enum 'InstrumentTypes'>
a
<InstrumentTypes.FIXEDPOINTCELL: 5>
type(a)
<enum 'InstrumentTypes'>
a == itype
False
Regarding the comments below it doesn't seem to be an enum-specific issue but a problem about multiple imports of the same module at different places. I'll try to clean up in the overall structure to get consistent imports.
As indicated in the comments to my question it was an issue about multiple imports of the same module, basically the same as in this answer. After removing a subdirectory of my main class directory from PYTHONPATH and correcting the import statements throughout my code, the enums are compared correctly to each other. Thank you for your contributions.
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 months ago.
Improve this question
There is no output on command line. Also the variable input cannot be set.
I've tried removing the variable from class, same result.
class test:
input = ""
def __init__(this, input):
this.input = input
def output(this):
print(this.input)
obj = test("hallo")
obj.output
I expect the output to be hallo.
You need to call the method, not just refer to it:
obj.output()
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 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()
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