IT Community - Software Programming, Web Development and Technical Support

Python resemble in its class syntax with other languages

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


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Python

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 05-22-2007, 04:00 AM
vigneshgets vigneshgets is offline
D-Web Genius
 
Join Date: Mar 2007
Posts: 904
vigneshgets is on a distinguished road
Question Python resemble in its class syntax with other languages

Hi Guys can anybody tell me Which of the languages does Python resemble in its class syntax?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 05-27-2007, 11:01 PM
santoshmalvi santoshmalvi is offline
D-Web Trainee
 
Join Date: Mar 2007
Location: Bangalore
Posts: 11
santoshmalvi is on a distinguished road
Default Re: Python resemble in its class syntax with other languages

Quote:
Originally Posted by vigneshgets View Post
Hi Guys can anybody tell me Which of the languages does Python resemble in its class syntax?
Unfortunately, this syntax is rather cumbersome, and furthermore tables do not support inheritance. Therefore Python introduced a new data type, the class (though it actually resembles an object in C++, not a class.)

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Basic Syntax of Regular Expressions senraj PHP Programming 12 02-08-2008 08:29 PM
How is JavaScript syntax like C / C++? itbarota HTML, CSS and Javascript Coding Techniques 1 09-13-2007 04:09 AM
What is the syntax for TextOut function in VC++ Win32 application? mobilegeek C and C++ Programming 3 08-09-2007 03:23 AM
Is there any way to use keyPressed method of Canvas class in Form class? bluesky Mobile Software Development 1 07-25-2007 06:02 AM
Oracle Cluster - Create and Alter Basic Syntax Murali Database Support 2 07-11-2007 06:40 AM


All times are GMT -7. The time now is 07:53 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0