Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'output': 0.04; 'compiler': 0.05; 'debug': 0.05; 'class,': 0.07; 'strings.': 0.07; 'suppose': 0.07; 'try:': 0.07; 'type,': 0.07; '[1,': 0.09; 'ast': 0.09; 'objects.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runtime': 0.09; 'subclass': 0.09; 'terry': 0.09; 'typeerror:': 0.09; 'def': 0.10; ':-)': 0.13; "'))": 0.16; "'user'": 0.16; 'b):': 0.16; 'declaration': 0.16; 'fuzzy': 0.16; 'input.': 0.16; 'inputs': 0.16; 'message-id:@dough.gmane.org': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'similarly,': 0.16; 'sorts': 0.16; 'still,': 0.16; 'value:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance': 0.17; 'integer': 0.17; 'variables': 0.17; 'jan': 0.18; '>>>': 0.18; 'input': 0.18; 'sort': 0.21; 'import': 0.21; 'produces': 0.22; 'universal': 0.22; 'errors': 0.23; 'testing': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'leave': 0.26; 'am,': 0.27; 'checking': 0.27; 'restrict': 0.27; 'header:X -Complaints-To:1': 0.28; '3.1': 0.29; 'declared': 0.29; 'str': 0.29; 'objects': 0.29; 'class': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'that.': 0.30; 'usually': 0.30; 'function': 0.30; 'on,': 0.30; 'code': 0.31; '(and': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'list': 0.35; 'list.': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; "i'll": 0.36; 'possible': 0.37; 'two': 0.37; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'object': 0.38; 'some': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'easy': 0.60; 'further': 0.61; 'real': 0.61; 'subject:, ': 0.61; 'strange': 0.62; 'limited.': 0.64; 'making': 0.64; 'become': 0.65; "'object'": 0.84; 'comparable': 0.84; 'max,': 0.84; 'received:fios.verizon.net': 0.84; 'restricted.': 0.84; 'items,': 0.91; 'kat': 0.91; 'subject:skip:E 10': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Encapsulation, inheritance and polymorphism Date: Tue, 17 Jul 2012 13:07:38 -0400 References: <3vnfd9-343.ln1@satorlaser.homedns.org> <-8SdnVrXGqie25jNnZ2dnUVZ7qKdnZ2d@bt.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342544885 news.xs4all.nl 6845 [2001:888:2000:d::a6]:45756 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25515 On 7/17/2012 10:23 AM, Lipska the Kat wrote: > Well 'type-bondage' is a strange way of thinking about compile time type > checking and making code easier to read (and therefor debug 'type-bondage' is the requirement to restrict function inputs and output to one declared type, where the type declaration mechanisms are usually quite limited. >>> def max(a, b): if a <= b: return a return b >>> max(1,3) 1 >>> max(3.3, 3.1) 3.1 >>> max('ab', 'aa') 'aa' >>> max([1,1], [1,0]) [1, 0] and so on, indefinitely. How easy is it to write the same in Java? Similarly, list.sort sorts any list as long as a < b works for all pairs of items in the list. Function max works for any two objects as long as 'a <= b' works. So the 'type' of a and b is 'mutually comparable with <='. How do you declare that in Java? How do you declare the type 'non-negative number'? In python, putting 'if input >= 0:' as the top effectively 'declares' that input must be a number and greater than 0. > but I'm not about to get into some religious war about declaring a variables > type. In Python, *all* data items have a class (type). Names in code do not have a type. When they become data items, they are strings. 'Variable' is a somewhat fuzzy term or concept in Python. Since every object is an instance of some class, every class is a subclass of class 'object', and an instance of a class is an instance of all its classess superclasses; every object is an instance of 'object'. So 'object' is the type of all inputs until further restricted. id(ob) produces an integer for all objects. str(ob) is intented to produce a string representation for all objects. The print functions calls str on all its inputs. > I'll just say that I prefer to devote testing efforts to the real > danger area which in my experience is 'user' input. > Clients look dimly on runtime errors however they occur and if I can > leave it to the compiler to check as much as possible then I'll take that. import ast a = ast.literal_eval(input('enter a value: ')) b = ast.literal_eval(input('enter a comparable value: ') try: print('the max of those two is ', max(a,b)) except TypeError: print(a, ' and ', b, ' are not comparable') I suppose > Still, I'm sure you're only kidding around with me :-) How easy was it to write max, or a universal sort in Java? -- Terry Jan Reedy