I am a newbie for python and try to understand class Inheritance.
aaabbb16 at hotmail.com
aaabbb16 at hotmail.com
Sat Oct 15 03:59:48 EDT 2011
On 10月15日, 上午12时04分, Chris Rebert <c... at rebertia.com> wrote:
> On Fri, Oct 14, 2011 at 11:20 PM, <aaabb... at hotmail.com> wrote:
> > Test.py
> > #!/usr/bin/python
> > from my_lib import test
> > class my_class(my_function.name):
>
> Why are you subclassing my_function.name and not just my_function?
try to inherit or change "name" attribute in "my_function".
>
> > def __initial__(self, name);
> > pass
>
> The initializer method should be named "__init__", not "__initial__".
ipad is so smart, it thinks I have spelling problem and
correct it. haha. sorry about it.
>
> > def test():
>
> You forgot to include "self" as a parameter, like so:
> def test(self):
right thanks!
>
> > print "this is a test"
>
> > If __name__ == '__maim__':
>
> That should be "__main__" with an N, not "__maim__" with an M.
> And "if" must be in all-lowercase.
mistyping
> > 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?
> > ---------------------------------------------------
> > my_lib.py
> > class my_function()
>
> You're missing a colon after the parentheses. Also, you're writing a
> class, not a function, so please rename the class something less
> confusing.
sure. i like call it from my_lib
>
> > Can anyone finish above code and let me try to understand
> > Class inheritance?
> > TIA.
>
> Have you read any Python tutorial? There are several basic errors in
> your code which would suggest that you haven't. You really ought to;
> it's well worth it.
> The Beginner's Guide links to 2 lists of tutorials:http://wiki.python.org/moin/BeginnersGuide
>
> There's also the python-tutor mailing list, which is specifically
> geared towards answering beginner questions:http://mail.python.org/mailman/listinfo/tutor
>
> Cheers,
> Chris
> --http://rebertia.com
Thanks Chris! I'll check that link.
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
TIA.
david
More information about the Python-list
mailing list