Re: typing: property/setter and lists?
On 03/11/2022 16.24, Paulo da Silva wrote: class C: def __init__(self): self.__foos=5*[0] @property def foos(self) -> list[int]: return self.__foos @foos.setter def foos(self,v: int): self.__foos=[v for __i in self.__foos] c=C() c.foos=5 print(c.foos) ___ mypy gives the following error: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]") To help us to help you please copy-paste the *exact* message - especially which line is in-question. The above code passes without complaint in PyCharm, and executes. However, the general rule?convention would be to establish type at the first mention of the identifier, eg def __init__(self): self.__foos:list[int] = 5 * [ 0 ] # or [ 0, 0, 0, 0, 0, ] Why the "__i", and not "i", or "_"? -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists?
On 03/11/2022 04:24, Paulo da Silva wrote: Hi! And a typing problem again!!! ___ class C: def __init__(self): self.__foos=5*[0] @property def foos(self) -> list[int]: return self.__foos @foos.setter def foos(self,v: int): self.__foos=[v for __i in self.__foos] c=C() c.foos=5 print(c.foos) ___ mypy gives the following error: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]") How do I turn around this? Quoting https://github.com/python/mypy/issues/3004: """ gvanrossum commented on Mar 15, 2017 IIRC we debated a similar issues when descriptors were added and decided that it's a perverse style that we just won't support. If you really have this you can add # type: ignore. """ -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
On 03/11/2022 00:25, Julieta Shem wrote: >> |OOP to me means only messaging, local retention and protection and >> |hiding of state-process, and extreme late-binding of all things. > > I'm wondering how Python fails to satisfy his definition. Python doesn't do any form of data protection/hiding. All attributes are public by default. In Smalltalk all attributes are private, with no way to make them public... Actually in C++/Java terms I believe they are "protected" because subclasses can access them(I think?). Also Python is not a purely OOP language, in that you can write functional and procedural code in Python if you wish. In Smalltalk thats notionally impossible because everything is an object. And all programming statements are messages to objects. Even an if/else test is a message to the boolean object: (5>3) ifTrue: ifFalse: ifTrue is a message to the boolean result of the expression which has a parameter of a block of code. It executes the block if the boolean is true. ifFalse is likewise a message to the same boolean object but only executes its block if the boolean is false. (Apologies if the syntax is out, its been a while since I wrote any Smalltalk!) Similarly loops are implemented as messages: whileTrue: to: do: So in Smalltalk absolutely everything is a message to an object. Python by comparison is much more traditional in form. It is possible to write procedural, non OOP code in Smalltalk but you really have to fight the system to do so. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
C++/Java class variables can be public, protected (accessible to class and subclasses) or private (accessible only to class). Of course the language protections can be hacked around. Python does conceptual private variables by using the single underscore: object._myvar is considered private From: Python-list on behalf of Alan Gauld Date: Thursday, November 3, 2022 at 6:45 AM To: Julieta Shem , [email protected] Subject: Re: an oop question *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** On 03/11/2022 00:25, Julieta Shem wrote: >> |OOP to me means only messaging, local retention and protection and >> |hiding of state-process, and extreme late-binding of all things. > > I'm wondering how Python fails to satisfy his definition. Python doesn't do any form of data protection/hiding. All attributes are public by default. In Smalltalk all attributes are private, with no way to make them public... Actually in C++/Java terms I believe they are "protected" because subclasses can access them(I think?). -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
On Thu, 3 Nov 2022 at 21:44, Alan Gauld wrote: > Also Python is not a purely OOP language, in that you can write > functional and procedural code in Python if you wish. In > Smalltalk thats notionally impossible because everything > is an object. And all programming statements are messages > to objects. In Python, everything is an object. Doesn't that equally mean that Python is purely OOP? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists? [RESOLVED]
Às 03:24 de 03/11/22, Paulo da Silva escreveu: Hi! And a typing problem again!!! ___ class C: def __init__(self): self.__foos=5*[0] @property def foos(self) -> list[int]: return self.__foos @foos.setter def foos(self,v: int): self.__foos=[v for __i in self.__foos] c=C() c.foos=5 print(c.foos) ___ mypy gives the following error: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]") How do I turn around this? Changing def foos(self) -> list[int]: to def foos(self) -> Union[list[int]]: fixes the problem. Not so elegant, however! Regards. Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
[email protected] (Stefan Ram) writes: > Julieta Shem writes: >>That's very interesting. Would you share the complete thread of e-mail? >>I would love to read it word for word. > > Yes, with pleasure! A quotation from my corresponding web page: > > (For technical reasons, the web page today gives > "https://www.purl.org/stefan_ram/pub/doc_kay_oop_en"; as the > "canonical URI", but both should work. I prefer the old one > with "http:", but am afraid that one day "http" might stop > to work.) [...] Nice! Thank you so much! -- https://mail.python.org/mailman/listinfo/python-list
Fwd: Issues in python 3.11.0 (64-bit) installation
-- Forwarded message - From: Suresh Babu Date: Thu, 3 Nov 2022, 16:37 Subject: Issues in python 3.11.0 (64-bit) installation To: Sir/ Madam, I downloaded the latest version of python i.e. python 3.11.0 ( 64-bit) in my laptop recently. But the " py launcher " and " available for all users " option is not working in the customization menu of python 3.11.0 . Kindly help me in solving this issue. My operating system is Windows 11. Kindly guide me in this regard. Yours sincerely, Sureshbabu. -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
Greg Ewing writes: > On 3/11/22 1:37 pm, Julieta Shem wrote: >> The code for computing the length of a Pair (which is really a linked >> list) happens to be the same for computing the length of a Stack. > > I would question whether that should be a method of Pair at all, > since it's not the length of the pair itself, but the length of > a structure built out of pairs. You make a lot of sense. > But in any case, the way to do this in a conceptually clean way > is for the length method of Stack to call whatever it is that > computes the length of a linked list of Pairs. This is what > the term "delegation" refers to. Thank you for the example. I agree with you. Perhaps I can reduce the class Pair to just being a pair as we know it, made of two things, then we create a class called Sequence, which is really a composition of Pairs, comes with a length method and we derive Stack from it. -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists? [RESOLVED]
On Fri, 4 Nov 2022 at 05:03, Paulo da Silva wrote: > Changing def foos(self) -> list[int]: to > def foos(self) -> Union[list[int]]: > fixes the problem. > Not so elegant, however! Wait, what?! Union[X, Y] means "X or Y" Union[X] means "X, but don't complain if it's a @property". Is that how it goes? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
Chris Angelico writes: > On Thu, 3 Nov 2022 at 21:44, Alan Gauld wrote: >> Also Python is not a purely OOP language, in that you can write >> functional and procedural code in Python if you wish. In >> Smalltalk thats notionally impossible because everything >> is an object. And all programming statements are messages >> to objects. > > In Python, everything is an object. Doesn't that equally mean that > Python is purely OOP? I think Alan Gauld pointed out that even syntax is an object in Smalltalk --- or was. An if-statement in Python is not an object. -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
On Fri, 4 Nov 2022 at 05:21, Julieta Shem wrote:
>
> Chris Angelico writes:
>
> > On Thu, 3 Nov 2022 at 21:44, Alan Gauld wrote:
> >> Also Python is not a purely OOP language, in that you can write
> >> functional and procedural code in Python if you wish. In
> >> Smalltalk thats notionally impossible because everything
> >> is an object. And all programming statements are messages
> >> to objects.
> >
> > In Python, everything is an object. Doesn't that equally mean that
> > Python is purely OOP?
>
> I think Alan Gauld pointed out that even syntax is an object in
> Smalltalk --- or was. An if-statement in Python is not an object.
Okay, fair; although I would be highly surprised if syntax is actually
an object ("flow control is an object", I would believe, though). Is
the concept "pass this message to this object" an object? Is the
message itself an object? Is it objects all the way down?
At some point, any language with objects in it is "object oriented" to
some extent, and after that, it's all a spectrum. Some people claim
that Java is "more object-oriented" than Python because all Java code
has to be in a class, and others counteract by saying that Python is
"more object-oriented" because every value in Python is a subclass of
object and has a type which is a subclass of type. I'm sure that
someone somewhere has claimed that Python "isn't object-oriented" on
the basis that len(x) is the WRONG way to put it, and it should be
x.length() instead.
On second thoughts, it's not a single spectrum, it's a
multidimensional thing that's so tangled up that it guarantees that
people can happily debate for years to come.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists? [RESOLVED ERRATA]
Às 05:32 de 03/11/22, Paulo da Silva escreveu: Às 03:24 de 03/11/22, Paulo da Silva escreveu: Hi! And a typing problem again!!! ___ class C: def __init__(self): self.__foos=5*[0] @property def foos(self) -> list[int]: return self.__foos @foos.setter def foos(self,v: int): self.__foos=[v for __i in self.__foos] c=C() c.foos=5 print(c.foos) ___ mypy gives the following error: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]") How do I turn around this? Changing def foos(self) -> list[int]: to def foos(self) -> Union[list[int]]: I meant, of course, def foos(self) -> Union[list[int],int]: Sorry. Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists? [RESOLVED]
Às 18:16 de 03/11/22, Chris Angelico escreveu: On Fri, 4 Nov 2022 at 05:03, Paulo da Silva wrote: Changing def foos(self) -> list[int]: to def foos(self) -> Union[list[int]]: fixes the problem. Not so elegant, however! Wait, what?! Union[X, Y] means "X or Y" Union[X] means "X, but don't complain if it's a @property". Is that how it goes? I'm sorry. A bad transposition of the text. I meant def foos(self) -> Union[list[int],int]: Regards. Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists?
Às 07:55 de 03/11/22, dn escreveu: On 03/11/2022 16.24, Paulo da Silva wrote: class C: def __init__(self): self.__foos=5*[0] @property def foos(self) -> list[int]: return self.__foos @foos.setter def foos(self,v: int): self.__foos=[v for __i in self.__foos] c=C() c.foos=5 print(c.foos) ___ mypy gives the following error: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]") To help us to help you please copy-paste the *exact* message - especially which line is in-question. The above code passes without complaint in PyCharm, and executes. However, the general rule?convention would be to establish type at the first mention of the identifier, eg def __init__(self): self.__foos:list[int] = 5 * [ 0 ] # or [ 0, 0, 0, 0, 0, ] Why the "__i", and not "i", or "_"? Just because of my personal taste. I know that __ means (for me) a "not used anymore" var and i is an indexer/counter/... Regards. Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: typing: property/setter and lists? [RESOLVED ERRATA]
On Fri, 4 Nov 2022 at 05:48, Paulo da Silva wrote: > > Às 05:32 de 03/11/22, Paulo da Silva escreveu: > > Às 03:24 de 03/11/22, Paulo da Silva escreveu: > >> Hi! > >> > >> And a typing problem again!!! > >> ___ > >> class C: > >> def __init__(self): > >> self.__foos=5*[0] > >> > >> @property > >> def foos(self) -> list[int]: > >> return self.__foos > >> > >> @foos.setter > >> def foos(self,v: int): > >> self.__foos=[v for __i in self.__foos] > >> > >> c=C() > >> c.foos=5 > >> print(c.foos) > >> ___ > >> > >> mypy gives the following error: > >> error: Incompatible types in assignment (expression has type "int", > >> variable has type "List[int]") > >> > >> How do I turn around this? > >> > > Changing def foos(self) -> list[int]: to > > def foos(self) -> Union[list[int]]: > I meant, of course, > def foos(self) -> Union[list[int],int]: > Ohhh! I thought this was triggering a strange quirk of the checker in some way... ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
[email protected] (Stefan Ram) writes: > Julieta Shem writes: >>I'll definitely look up the Liskov >>substitution principle and try to understand it. > > I found the LSP to be very confusing: > > First, it's was hard for me to actually get a clear source > (citation) for it. What exactly are the words of Barbara > Liskov that are called "LSP"? > > Then, I found some words that that might possibly be the LSP > in the words of Barbara Liskov: > > |If for each object o1 of type S there is an object o2 of > |type T such that for all programs P defined in terms of T, > |the behavior of P is unchanged when o1 is substituted for o2 > |then S is a subtype of T. > > This uses nested quantifiers, somewhat like > > ( ∀(o1∈S) ∃(o2∈T) ∀(P∈P(T)) B(P(o2))=(P(o1)) )==> S < T > > . And such a proposition is hard for me to understand! For me too. > Later, I looked at a book in a bookstore; it was a book > about programming by Barbara Liskov that came out after the > LSP was already mentioned often, and as far as I could see, > that book did not mention the LSP at all, although it > actually had a chapter about subtypes! Do you remember the book's title? > So, here's my personal principle, that I use instead of the LSP: > > An object-oriented program is not complete without proper > documentation, i.e., contracts. The documentation of a class > must be true for all objects of this class and for all objects > of all direct and indirect subclasses. That's much easier to ``parse'' indeed. Thanks for the contribution. (The empty documentation seems to satisfy the principle.) > . If this was too long, one could abbreviate this to just: > > Class contracts must hold for subclasses. > > . I think the original LSP unfortunately tried to avoid > references to contracts and just talks about code. -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
[email protected] (Stefan Ram) writes: > Julieta Shem writes: [...] > 2. a. 1. Remark > > One can observe this ease especially when one defines a new > class with a standard verb and then standard procedures > "magically" use this new method, as in: > > class MyNewClass: > def __str__( self ): > return "Howdy!" > > print( MyNewClass() ) > > How can "print" possibly know about the method "__str__" > I just defined if "print" was written long before I defined > my class? <-- A beginner could ask this in bewilderment! That's a good question. > 2. b. Adding a New Verb (Procedure) in Object-Oriented Programming > > In object-oriented programming adding a new verb (a new > "procedure") is hard. Assume that now we would like to add > another verb such as "emit", say "length". All classes would > have to be changed and a new method definition for "length" > would have to be added to them! Some classes might even be > standard classes from libraries we can't easily change. > So this clearly violates the Open-Closed-Principle! > > 3. Comments > > So, this would suggest to use procedural programming when > one foresees the need to add more object-specific procedures > later and object-oriented programming when one foresees the > need to add more types later. > > The problems with OOP which make adding new verbs violate > the open-closed principle possibly would not occur in > a language where one could add new methods to a library > class in a user program. Thank you! That did clarify everything for me! (I'll find a way to read Martin's book!) -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems with IDLE in Windows 8.1 and installer x86 Version 3.10.8
On 11/3/22, [email protected] wrote: > Is there a reason, why it is not installed? Its the same check mark in the > installer like IDLE… Did you try what I suggested? Modify the installation to remove the tkinter/IDLE component. Then modify it again to select the component to be reinstalled. Also, try to repair the installation. This may reset any DLLs or extension modules that were missing or that were the wrong version. Ignore the suggestion from Nithish to install tkinter via pip. tkinter is part of the standard library and cannot be installed via pip. There is no tkinter package on the Python package index (pypi.org). -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
On 03/11/2022 18:29, Chris Angelico wrote: > On Fri, 4 Nov 2022 at 05:21, Julieta Shem wrote: >> >> Chris Angelico writes: >> >>> On Thu, 3 Nov 2022 at 21:44, Alan Gauld wrote: Also Python is not a purely OOP language, in that you can write functional and procedural code in Python if you wish. In Smalltalk thats notionally impossible because everything is an object. And all programming statements are messages to objects. >>> >>> In Python, everything is an object. Doesn't that equally mean that >>> Python is purely OOP? >> >> I think Alan Gauld pointed out that even syntax is an object in >> Smalltalk --- or was. An if-statement in Python is not an object. > > Okay, fair; although I would be highly surprised if syntax is actually > an object The syntax isn't, it is a specification, but the AST certainly is and you can instantiate it and examine it from code. But the context here was Why *Alan Kay* doesn't include Python with Smalltalk as being OOP. There are plenty others who would call it so. But as for everything being an object, that's true but it doesn't alter the fact that the default mode of python programming is not to structure the code as a set of communicating objects (even if at some level everything is an object) but as a set of hierarchical functions. And fundamentally that's what Kay means by OOP. The program (not the language!) is built around objects. One of the greatest barriers to the adoption of OOP is the confusion between OOP and OOPL. And sadly the majority of attention has been on OOPL rather than OOP... > the concept "pass this message to this object" an object? Is the > message itself an object? Is it objects all the way down? At some point you hit C/Assembler. But it is astonishing just how far down the objects go in Smalltalk. But the control flow etc are fully OOP as per my last message. It if quite possible to subclass Boolean and override the whileTrue method to do something completely different to the normal while loop behaviour. Is it wise? No. But it is definitely possible! > At some point, any language with objects in it is "object oriented" to > some extent, and after that, it's all a spectrum. And this is exactly the problem. In the 80s we had a fairly clean concensus of what OOP meant and several different language approaches to that. But when C++ became popular(the defacto standard) peple started focussing on the language features and totally missed that Object Oriented Programming means writing programs that consist of objects communicating by messages. The language features(even classes) are merely implementation details. (I suspect this was partly driven by the fact that many university lecturers at the time didn't really grok OOP and teaching language features was much easier than teaching a new way of thinking about problems!) This was compounded when someone (Booch maybe?) came up with a set of criteria to determine whether a language was a "true OOPL" or merely "Object Based" (Ada and VB they're looking at you!) But frankly that was an irrelevance to OOP. You can do OOP (and I have!) in assembler and in COBOL - you just need to follow some agreed ground rules. It's a lot easier with an OOPL but it's not required. > multidimensional thing that's so tangled up that it guarantees that > people can happily debate for years to come. Exactly so. During the 90's there came to be at least 3 different camps of OOP evangelists and the OOP world slowly coalesced on a kind of hybrid amalgam with multiple names for the same feature and disagrements about which features are required or not to really be OOP. And as a result the whole concept of OOP has been muddied to the point that almost nobody does it anymore apart from (possibly) the Smalltalk crowd who have always smugly sat above the general throng content in the knowledge that theirs is the one true OOP! :-) Which brings us back to Alan Kay... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Challenge-week 4: Groups of solutions, starts today!
A virtual event run by the Auckland Branch of the New Zealand Python Users' Group. Details from the Meetup site: https://www.meetup.com/nzpug-auckland/events/289531194/ where you may also sign-up. It's Week 4 of the Smart Iterator Challenge! We've looked at taking our early results and generalising them to suit various solutions. The next view is how a range of similar solutions might be grouped - and re-shaped to enable us to re-use and share code. We commence with a review of Challenge-week 3 and sample-code to download and compare with your own efforts. This time there is no tutorial-content. Even the provided testing/proofs are rather sparse - and really only there to make sure you're on-the-right-track. In many ways, the challenge is not so much writing code, as it is designing code-solutions. Putting code-modules together, and then pulling them apart to satisfy groups of solutions. Ooh mysterious! This Challenge will interest Python-Journeymen, and Python-Apprentices ready to move-on from ‘the basics’. As usual, there are "Extra Credit" challenges which will 'push' Python-Masters and advanced Journeymen. It's not a requirement that you have completed the previous challenges - but they will help. Challenge-week 4 starts with either your own or a provided template-script. So, you don't have to have completed Challenge-weeks 1, 2, and 3 (but it would help - there's nothing to stop you from 'catching up' and gaining full benefit from the exercise). There's some 'template code' which will enable starting from this week. Challenge Schedule: Groups of solutions Starting: Sat 5 Nov Office Hours: 1830*, Wed 9 Nov Concluding: midnight after Sun 13 Nov * all times NZDT (UTC+13) Are you up for a challenge? Regards =dn (for Pete and DJ) -- https://mail.python.org/mailman/listinfo/python-list
Re: an oop question
https://www.oreilly.com/library/view/beginning-c-30/9780470261293/9780470261293_a_short_history_of_object-oriented_progr.html -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Issues in python 3.11.0 (64-bit) installation
On 04/11/2022 04.20, Suresh Babu wrote: I downloaded the latest version of python i.e. python 3.11.0 ( 64-bit) in my laptop recently. But the " py launcher " and " available for all users " option is not working in the customization menu of python 3.11.0 . Kindly help me in solving this issue. My operating system is Windows 11. Kindly guide me in this regard. Has the documentation left questions? https://docs.python.org/3/using/windows.html -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Issues in python 3.11.0 (64-bit) installation
I just downloaded the 64-bit Windows installer. On my Windows 10 machine, both "py launcher" and "available for all users" options were available. They were available whether I checked the "administrative" box or not. Note that when I unchecked "administrative", then the "available for all users" box became unchecked. That would make sense. When you wrote "option is not working", what did you mean by "not working"? - 1. The options were disabled (i.e., grayed out); 2. The options seemed to be available but the installation seemed to ignore them; 3. The options were completely missing from the wizard; 4. Something else. On 11/3/2022 9:44 PM, dn wrote: On 04/11/2022 04.20, Suresh Babu wrote: I downloaded the latest version of python i.e. python 3.11.0 ( 64-bit) in my laptop recently. But the " py launcher " and " available for all users " option is not working in the customization menu of python 3.11.0 . Kindly help me in solving this issue. My operating system is Windows 11. Kindly guide me in this regard. Has the documentation left questions? https://docs.python.org/3/using/windows.html -- https://mail.python.org/mailman/listinfo/python-list
