Reputation: 10061
for example, if I have the following classes
class Part(object):
def __init__(self,part_number,description,rev=None,color=None):
self.dims = {}
self.dim_id = 0
self.rev = rev
if type(color) is tuple:
self.color=(color[0]/255.,color[1]/255.,color[2]/255.)
else:
self.color = color
def add_dimension(self,description,value,tol,tol_type = 'sym',dwg_sheet = None, dwg_zone = None,quality = 3):
self.dims[description] = Dimension(description=description,part=self,value=value,tol=tol,tol_type=tol_type,quality=quality,dwg_sheet=None,dwg_zone=None)
def __getattr__(self, description):
return self.dims[description]
class Dimension(object):
def __init__(self,part,value,tol,tol_type = 'sym', dwg_sheet = None, dwg_zone = None, quality = 3, description = None):
self.value = value
self.part=part
self.tol = tol
self.tol_type = tol_type
self.description = description
self.quality = quality
self.sigma = self.tol/float(quality)
def __str__(self):
return self.description
Then I run the following code:
a = Part('pn','this is a part')
a.add_dimension('this_is_a_dimension',1.00,0.05)
print a.this_is_a_dimension
It returns:
this_is_a_dimension
the problem is that when I try to do tab completion after typing "a.
", I get only the following options
a.add_dimension
a.color
a.dim_id
a.dims
a.rev
I would like to be able to tab-complete to see my new parameter. It would be similar to how column names in Pandas Dataframes work.
Any thoughts?
Upvotes: 2
Views: 226
Reputation: 22681
You can. You must implement a __dir__()
method for your class, as discussed here.
This is an example of it working:
class bar(object):
def __init__(self,):
self.a = 4
self._morelements=['b']
def __dir__(self):
return sorted(set(dir(type(self)) + list(self.__dict__) + self._morelements))
foo=bar()
Now if you write foo.
and press TAB in Ipython you will have as available members both a
and b
, although b
does not exist.
The code for __dir__()
and a discussion of the limitations of this approach is here. In your case, you want to add description
to the list self._morelements
inside the function add_dimension()
.
EDIT:
And of course you also need to initialize self._morelements=[]
in the __init__()
, and add the __dir__()
function of the example above to your class Part
Upvotes: 5
Reputation: 14360
Python is interpreted. So, it only knows about code it has already interpreter. An example of this is: if you try to run the code you posted, you will get an error at the line:
self.dims[description] = Dimension(description=description,part=self,value=value,tol=tol,tol_type=tol_type,quality=quality,dwg_sheet=None,dwg_zone=None)
the error: global name 'Dimension' is not defined. That's because you define the class Dimension
after. At the time the interpreter reach that line, the class Dimension
does not exists.
this_is_a_dimension
attribute of a
only exists at run time and after the line:
a.add_dimension('this_is_a_dimension',1.00,0.05)
For accomplish what you want, you'll need some IDE that interprets the python code at edit time.
Upvotes: 0