I am working through trying to learn to program in Python and am focused on getting a better handle on how to use Standard and other modules. The dir function seems really powerful in the interpreter but I wonder if I am missing something because of my lack of OOP background. Using S.Lotts book I decided to use his Die class to learn more about syntax and use of classes and instances.
Here is the original code:
class Die(object):
''' simulate a six-sided die '''
def roll(self):
self.value=random.randrange(1,7)
return self.value
def getValue(self):
return self.value
I was looking at that and after creating some instances I wondered if the word value was a keyword somehow and what the use of the word object in the class statement did and so I decided to find out by changing the class definition to the following:
class Die():
''' simulate a six-sided die '''
def roll(self):
self.ban=random.randrange(1,7)
return self.ban
def getValue(self):
return self.ban
That change showed me that I got the same behavior from my instances but the following methods/attributes were missing from the instances when I did dir:
'__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
_repr__', '__setattr__', '__str__', '__weakref__'
I also figured out that when I did a dir on an instance I had an additional keyword-ban which I finally figured out was an attribute of my instance. This helped me understand that I could use d1.ban to access the value of my instance. The only reason I could figure out that this was an attribute was I typed d1.happy and got an AttributeError I figured out that d1.GetValue was a method attached to Die because that is what the interpreter told me.
So when I am trying to use some complicated but helpful module like BeautifulSoup how can I know which of the things that are listed are attributes of my instance or methods of my instance after typing dir(instance). I would need to know this because this poking around has taught me that with attributes I am calling the result of a method and with methods I am invoking a function on my instance.
This question is probably too wordy but it sure did help me better understand the difference between attributes and methods. Specifically, when I look at the result of calling dir on an instance of my Die class I see this
['__doc__', '__module__', 'ban', 'getValue', 'roll']
So it would seem useful to know by looking at that list which are attributes and which are methods without having to resort to trial and error or result to typing in hasattr(myInstance,suspectedAttributeName).
After posting the question I tried
for each in dir(d1):
print hasattr(d1,each)
which tells me strictly speaking that all methods are attributes. but I can't call a method without the () so it seems to me that the hasattr() is misleading.
Instead of: "print hasattr(d1,each)
", try: "print each, type(getattr(d1,each))
". You should find the results informative.
Also, in place of dir()
try help()
, which I think you're really looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With