473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Example for Overridding

440 256MB
Hi,

Could anybody help me with a sample code for function overidding.

Thanks & Regards
PSB
Feb 28 '07 #1
3 1000
bartonc
6,596 Expert 4TB
Hi,

Could anybody help me with a sample code for function overidding.

Thanks & Regards
PSB
Just like you'd expect:
Expand|Select|Wrap|Line Numbers
  1. class SuperClass(object):
  2.     def func1(self):
  3.         print 1234
  4.  
  5. class SubClass(SuperClass):
  6.     def func1(self):
  7.         print 5678
  8.         SuperClass.func1(self)
  9.  
  10. sc = SubClass()
  11. sc.func1()
Feb 28 '07 #2
psbasha
440 256MB
Just like you'd expect:
Expand|Select|Wrap|Line Numbers
  1. class SuperClass(object):
  2.     def func1(self):
  3.         print 1234
  4.  
  5. class SubClass(SuperClass):
  6.     def func1(self):
  7.         print 5678
  8.         SuperClass.func1(self)
  9.  
  10. sc = SubClass()
  11. sc.func1()
Hi,

Thanks for the example.

In case of C++ we have a Pointer concept and we can play with Base Class Object and Derived class objects to access the common methods or overriden methods thru thier Objects.Is it possible in Python to get the same functionality by assigning the derive class object to the base class and get access to the methods of the derived classes.As shown in the below example.If possible could you please help me in getting the sample code.

Example in C++ :

class A {
virtual void f() { cout << "Class A" << endl; }
};

class B:public A {
void f(int) { cout << "Class B" << endl; }
};

class C:public B {
void f() { cout << "Class C" << endl; }
};

int main() {
B b; C c;
A* pa1 = &b;
A* pa2 = &c;
// b.f();
pa1->f();
pa2->f();
}
The following is the output of the above example:

Class A
Class C

Regards
PSB
Feb 28 '07 #3
bvdet
2,851 Expert Mod 2GB
In this case, BasisTransToGlobal() is based on Plane3D which is based on object. BasisTransToGlobal() inhehits all of Plane3d()'s methods. Plane3D() overloaded __repr__ is overridden.
Expand|Select|Wrap|Line Numbers
  1. class BasisTransToGlobal(Plane3D):
  2.  
  3.     def __init__(self, vN, vA, vB, vR=Point(0,0,0)):
  4.         self.vN = vN
  5.         self.vA = vA
  6.         self.vB = vB
  7.         self.vR = vR
  8.         # local basis aligned with plane defined
  9.         Plane3D.__init__(self, vN, vA, vB)
  10.  
  11.         if chk_type([vR, ]):
  12.  
  13.             if self.N_len > 0.0:
  14.  
  15.                 # Unit vector normal to defined plane, local basis 'Z'
  16.                 self.N = self.N_uv
  17.  
  18.                 # Unit vector between vN and vA, local basis 'X'
  19.                 self.A = self.d0
  20.  
  21.                 # Unit cross product vector, local basis 'Y'
  22.                 self.B = self.cross_product(self.N, self.A)
  23.  
  24.                 # global coordinate vector
  25.                 self.R = self.translate(vR.x, vR.y, vR.z) + vN
  26.  
  27.             else:
  28.                 Warning("The points are collinear ***INVALID***")
  29.                 self.R = None
  30.         else:
  31.             raise TypeError, "Arguments must be <type 'point'>"
  32.  
  33.     def translate(self, X, Y, Z):
  34.         ....................................................................................
  35.         # global coordinate vector with respect to local basis origin
  36.         return Point(R1.x*M, R1.y*M, R1.z*M)
  37.  
  38.     def __repr__(self):
  39.         return 'BasisTransToGlobal(Point(%s), Point(%s), Point(%s), Point(%s))' % (self.vN, self.vA, self.vB, self.vR)  
Feb 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
5
by: relaxedrob | last post by:
Hi All! I ran the following w3c example through my WSDL parser (SOA Editor from Cape Clear): http://www.w3.org/TR/wsdl#_rpcexample It told me that there were a bunch of erros about no...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
11
by: ajikoe | last post by:
Hello, I used Visual C# Standard Edition. I want to comment my program using xml commentary method, I don't know why if I use value and example tag, it is not working / showed in the html...
6
by: cj | last post by:
Lets just take this example I'm looking at now. I'm looking at the help screen titled .NET Framework Class Library FolderBrowserDialog Class . It gives an example at the bottom that begins with:...
8
by: Scott M. | last post by:
Where will code that is preceded with: /// <example> /// some comments /// </example> actually show up? I can see my <summaryand <remarkscode showing up in the code comment pages and in...
25
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever...
6
by: Guy Macon | last post by:
While I agree with the sentiment, the oringinal title on this thread ("OT: Specially for , why you should always use example.com for obfuscating domains") is wrong. There are other reserved domain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.