I am a newbie for python and try to understand class Inheritance.
DevPlayer
devplayer at gmail.com
Sat Oct 15 17:31:20 EDT 2011
On Oct 15, 2:20 am, aaabb... at hotmail.com wrote:
> Test.py
> -----------
> #!/usr/bin/python
>
> from my_lib import my_function
>
> class MyClass(my_function): # usually class names start capital
>
> """We know you're not forgetting to document."""
>
> def __init__(self, name):
> super(MyClass, self).__init__(name)
> # self.name = name automatically added from base class
>
> def test(self):
> print "this is a test", self.name
>
> def __call__(self, name):
>
> # if you are subclassing something named my_function
> # then other Python coders -might- expect the subclass
> # to behave like a function.
>
> print "Now this class acts like a function %s" % name
> print "because it has a def __call__(self,...)."
> return True # not needed but because functions do stuff
>
> If __name__ == '__main__':
> myclass_instance = MyClass('David') # confusing isn't it to subclass something called my_function
> ================================================
> my_lib.py
> ---------
> class my_function(object):
>
> """This class acts like a function."""
>
> def __init__(self, name):
> self.name = SpamSpamAndEggs(name)
>
> def __call__(self):
> # do stuff
> return self.name.upper()
> ...
>
> -david
I editted your code to something closer to what is expected.
Not a law but just a recommendation.
Also defining a class my_function(..): --might-- be confusing to
others as in Python there are things called functions that look like
this:
def my_function(name):
print "Hi %s" % name
note the use of "def" and not "class"
Of course if you are making classes that represent custom application
"functions" verse Python "functions" then yeah, calling your class:
class MyFunction(object): ... makes sense
I included the __doc__ "You are documenting" stuff because you seem so
new. Otherwise when posting here they're not expected to be in these
posts.
More information about the Python-list
mailing list