On Sat, Oct 15, 2011 at 12:59 AM, <aaabb...@hotmail.com> wrote: > On 10月15日, 上午12时04分, Chris Rebert <c...@rebertia.com> wrote: >> On Fri, Oct 14, 2011 at 11:20 PM, <aaabb...@hotmail.com> wrote: <snip> >> > my_class.main() >> >> Your class doesn't define any method named "main" (you only defined >> test() and __initial__() ), so this call will fail. > how to do it?
You'd define a method named "main", just like with "test". You would then call it by doing: my_class().main() <snip> > Test.py > #!/usr/bin/python > from my_lib import p_test > class my_class(p_test.name): > def __initial__(self, name): > pass > def test(self): > print "this is a test" > > If __name__ == '__main__': > my_class.main() > --------------------------------------------------- > my_lib.py > class p_test() > ....... > ........ > > Can anyone finish it and give me a demo. > Class inheritance? > for this case, it inherit/change p_test "name" attribute. > I try to quick understand how to inherit parent attribute my_lib.py: class ParentClass(object): def __init__(self, name): self.name = name self.species = "Human" test.py: from my_lib import ParentClass class MyClass(ParentClass): def __init__(self, name): super(MyClass, self).__init__(name + " Jones") def print_name(self): print "My name is", self.name print "And I am a", self.species MyClass("Bob").print_name() Note that classes are conventionally named using CamelCaseLikeThis instead of underscore_separated_words. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list