LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-27-2013, 04:04 PM   #46
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453

Here is modified code from post #28, from under UPDATE:

Code:
sergei@amdam2:~/junk/python_work> cat -n main.py
     1  def not_to_be_called(msg):
     2    print "not_to_be_called msg: %s\n" % (msg)
     3
     4  def main():
     5
     6    import car
     7
     8    print "%s\n" % (__name__)
     9
    10    fancy_car = car.createCar('silver', 300)
    11    fancy_car.setDriver('long legged blond')
    12
    13    humble_car = car.createCar('dark green', 150)
    14    humble_car.setDriver('poor unemployed')
    15
    16    color = fancy_car.getColor('what a fancy car !')
    17    print "fancy car color is %s" % color
    18
    19    power = fancy_car.getPower('what a powerful car !');
    20    print "fancy car engine power is: %g" % power
    21
    22    driver = fancy_car.getDriver();
    23    print "in fancy car the driver is: %s" % driver;
    24
    25
    26    color = humble_car.getColor('what a modest person')
    27    print "fancy car color is %s" % color
    28
    29
    30    power = humble_car.getPower('what an economical car');
    31    print "fancy car engine power is: %g" % power
    32
    33    driver = humble_car.getDriver();
    34    print "in humble car the driver is: %s" % driver;
    35
    36
    37  if __name__ == "__main__":
    38    main()
sergei@amdam2:~/junk/python_work> cat -n car.py
     1  import inspect
     2  import __main__
     3
     4
     5
     6  def createCar(color, power):
     7      _driver = [None]
     8
     9      def _warn(msg, *args):
    10          frameinfo = inspect.getframeinfo(inspect.currentframe().f_back)
    11          filename = frameinfo.filename
    12          line_number = frameinfo.lineno
    13          if len(args) == 1:
    14              msg = msg % (args,)
    15          elif args:
    16              msg = msg % args
    17          print "#!#!#!# %s at line number %d in %s file" % (
    18              msg, line_number, filename)
    19
    20      class Car(object):
    21          def __repr__(self):
    22              return repr({
    23                  'color': color,
    24                  'power': power
    25              });
    26
    27          def getColor(self, aux_msg):
    28              __main__.not_to_be_called("calling from car");
    29              _warn('in %r, color=%s, msg=%s', self, color, aux_msg)
    30              return color
    31
    32          def getPower(self, aux_msg):
    33              _warn('in %r, power=%s, msg=%s', self, power, aux_msg)
    34              return power
    35
    36          def setDriver(self, driver):
    37              _driver[0] = driver
    38
    39          def getDriver(self):
    40              return _driver[0]
    41
    42      return Car()
sergei@amdam2:~/junk/python_work> ~/AFSWD/install/Python-2.7.3/binsh/python main.py
__main__

not_to_be_called msg: calling from car

#!#!#!# in {'color': 'silver', 'power': 300}, color=silver, msg=what a fancy car ! at line number 29 in /home/sergei/junk/python_work/car.py file
fancy car color is silver
#!#!#!# in {'color': 'silver', 'power': 300}, power=300, msg=what a powerful car ! at line number 33 in /home/sergei/junk/python_work/car.py file
fancy car engine power is: 300
in fancy car the driver is: long legged blond
not_to_be_called msg: calling from car

#!#!#!# in {'color': 'dark green', 'power': 150}, color=dark green, msg=what a modest person at line number 29 in /home/sergei/junk/python_work/car.py file
fancy car color is dark green
#!#!#!# in {'color': 'dark green', 'power': 150}, power=150, msg=what an economical car at line number 33 in /home/sergei/junk/python_work/car.py file
fancy car engine power is: 150
in humble car the driver is: poor unemployed
sergei@amdam2:~/junk/python_work>

I have added 'not_to_be_called' function to 'main.py' and I can easily (and I do) call it from 'car.py'.

In my Perl code it's impossible - because neither 'main.pl', nor 'car_creator.prl' pollute global namespace. And by Perl construction lexical variables of 'main.pl' are invisible in 'car_creator.prl' and vice versa.

This complete isolation (except for the export -> input anonymous code reference) was the design goal. This design goal is not met in the Python translation.
 
Old 02-28-2013, 08:02 AM   #47
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 414

Rep: Reputation: 173Reputation: 173
This thread is going nowhere. EOT from my side.
 
Old 03-04-2013, 10:54 PM   #48
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by mina86 View Post
This thread is going nowhere. EOT from my side.
Well, the concrete proof of lack of protection in Python from execution of code that isn't supposed to be executed is somewhere.

In my work I had to let users of a "big" tool to write "small" custom code that is needed to implement the concretes users' goals, and I needed at the same time to be able to preserve "integrity" of the tool, i.e. to not let users do what they are not supposed to do.
 
Old 03-06-2013, 09:56 AM   #49
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
Speaking of "Perl vs. Python," as a design choice ... sometimes you make a choice because of the peculiar expressiveness of a particular viable language for doing exactly what you want to do at the moment. It might be the availability of a well-tested package, but sometimes it's "the language itself."

In the case of Python, one of those "deal makers" is the language's built-in handling of lists, a la LISP. For example, you can easily do a "list comprehension" in Python. (Google it.) Perl version 6 promises that support, too, but we've been waiting for that language to make it out of the design-temple for about five years now. A rather lively discussion's been going on, off and on, at PerlMonks concerning "combinatorics," and doing this in Perl takes either a lot of programming or the use of a package where someone else did a lot of programming for you. Meanwhile, the Python language expresses this same idiom natively.

Programming languages, like their designers and champions, are not the same; not at all. One of the very good reasons for studying them ... and I don't just mean, "in school" ... is that all tools are especially good at some things and not so especially good at others. If this is your chosen profession, and your toolbox has a plenitude of good tools in it (as Linux does), then you need to invest some time, all the time, in learning more than a little bit about each one.

Also, you rarely (if ever) will stumble into a project that is truly "virgin." Nearly all the time, the nemesis that will profitably occupy your attention for years to come already exists, in some language or another.

Last edited by sundialsvcs; 03-06-2013 at 09:57 AM.
 
Old 03-06-2013, 12:03 PM   #50
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 414

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by sundialsvcs View Post
For example, you can easily do a "list comprehension" in Python.
You can do that in Perl with map and grep but I do agree that Python's syntax is more convenient.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python related: How to access a Perl script behind a firewall from Python? vxc69 Programming 8 12-14-2010 08:32 AM
Perl Vs python knockout_artist Linux - Newbie 6 10-15-2008 12:06 PM
My needs: Perl vs. Python dave201 Programming 25 08-12-2007 12:13 AM
perl vs python yenonn Programming 6 08-01-2006 06:44 AM
Perl or Python ! linuxlover1 Programming 13 04-19-2004 08:33 AM


All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration