I am a newbie for python and try to understand class Inheritance.

A

aaabbb16

Test.py
#!/usr/bin/python
from my_lib import my_function
class my_class(my_function.name):
def __initial__(self, name);
pass
def test():
print "this is a test"

If __name__ == '__maim__':
my_class.main()
---------------------------------------------------
my_lib.py
class my_function()
........
.........

Can anyone finish above code and let me try to understand
Class inheritance?
TIA.
-david
 
C

Chris Rebert

Test.py
#!/usr/bin/python
from my_lib import my_function
class my_class(my_function.name):

Why are you subclassing my_function.name and not just my_function?
   def __initial__(self, name);
        pass

The initializer method should be named "__init__", not "__initial__".
   def test():

You forgot to include "self" as a parameter, like so:
def test(self):
      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.
   my_class.main()

Your class doesn't define any method named "main" (you only defined
test() and __initial__() ), so this call will fail.

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.
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
 
C

Chris Rebert

For instance, let's say you want to deal with shapes.  You can define a
shape via a class

class Shape(object):
   """ Base shape class """
Now we get into inheritance.  Let's suppose that we want a specific type of
shape.  For instance, a circle (that is defined by an infinite number of
vertices).  In this case, a circle is still a shape, so it should have every
attribute that a normal shape has.  Thus, we can define a circle class as
follows:

class Circle(Shape):
   """ Circle inherits from Shape """
   number_vertex_points = 1000 # Default number of vertex points I want to
define a circle
   def __init__(self, center, radius):
      """ Define the self.vertices here to traceout a circle as closely as
you want """

Now, each time we instantiate a Circle class, that class has every attribute
that Shape has, in addition to any additional attribute you give to Circle
(and if the same attribute is defined in both places, the definition in
Circle overrides that definition).  Thus, in this case we can definea
Circle with a center and radius (much easier than vertices!), and we can
tell Circle how we want the vertices defined to get as close an
approximation to a circle as we want.

Sidenote: It's funny that the shapes example gets used so often,
despite the fact that pursuing it much further so easily leads to the
Circle-Ellipse / Rectangle-Square problem.

Cheers,
Chris
 
A

aaabbb16

Why are you subclassing my_function.name and not just my_function?
try to inherit or change "name" attribute in "my_function".
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.
You forgot to include "self" as a parameter, like so:
    def test(self): right thanks!



That should be "__main__" with an N, not "__maim__" with an M.
And "if" must be in all-lowercase. mistyping


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'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
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
 
C

Chris Rebert

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()

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
 
D

DevPlayer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top