This is a discussion on Python resemble in its class syntax with other languages within the Python forums, part of the Software Development category; Hi Guys can anybody tell me Which of the languages does Python resemble in its class syntax?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Quote:
A class is like a table, except it uses a shorter, more familiar access syntax (x.real instead of x["real"]) and can inherit from other classes. The syntactical definition of a class also differs significantly from a table expression. Here is how the same example might appear using a class: class complex: real = 0 imag = 0 def add(x,y): z = copy(complex) z.real = x.real + y.real z.imag = x.imag + y.imag return z x = copy(complex) x.real = 5 x.imag = 3 y = copy(complex) y.real = 2 y.imag = -2 z = x.__dict__["add"](x,y) # x.add(x,y) z.real # prints 7 z.imag # prints 1 The caveat here is that Python does not allow us to call x.add directly, so we must use a kludge. Python does, however, allow us to embed the definition of add inside the class. Here is how it might look otherwise, using an external function as before. Syntactical differences aside, these two complex-number abstractions work in exactly the same way. In fact, Python allows us to blur the difference by treating a class like a table (but not vice-versa), via the __dict__ attribute or vars() function: x = copy(complex) x.real = 5 x.imag = 3 x.__dict__["real"] # prints 5 vars(x)["imag"] # prints 3 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Php Basic Syntax of Regular Expressions | senraj | PHP Programming | 12 | 02-08-2008 07:29 PM |
| How is JavaScript syntax like C / C++? | itbarota | HTML, CSS and Javascript Coding Techniques | 1 | 09-13-2007 03:09 AM |
| What is the syntax for TextOut function in VC++ Win32 application? | mobilegeek | C and C++ Programming | 3 | 08-09-2007 02:23 AM |
| Is there any way to use keyPressed method of Canvas class in Form class? | bluesky | Mobile Software Development | 1 | 07-25-2007 05:02 AM |
| Oracle Cluster - Create and Alter Basic Syntax | Murali | Database Support | 2 | 07-11-2007 05:40 AM |