Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79968 > unrolled thread

Classes

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-10-30 16:16 -0400
Last post2014-10-31 14:56 -0400
Articles 20 on this page of 68 — 20 participants

Back to article view | Back to comp.lang.python


Contents

  Classes   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 16:16 -0400
    Re: Classes sohcahtoa82@gmail.com - 2014-10-30 13:33 -0700
      Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:00 -0400
    Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-30 13:34 -0700
      Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:00 -0400
    Re: Classes Larry Hudson <orgnut@yahoo.com> - 2014-10-30 14:28 -0700
      Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-30 17:34 -0400
        Re: Classes sohcahtoa82@gmail.com - 2014-10-30 18:21 -0700
        Re: Classes alister <alister.nospam.ware@ntlworld.com> - 2014-10-31 09:59 +0000
          Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 10:07 -0400
    Re: Classes   "ast" <nomail@invalid.com> - 2014-10-31 10:05 +0100
      Re: Classes   "ast" <nomail@invalid.com> - 2014-10-31 10:18 +0100
        Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-10-31 20:21 +1100
      Re: Classes   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 10:05 -0400
        Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-31 08:47 -0600
          Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 15:49 +0000
            Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:31 -0400
              Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 03:37 +1100
                Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:47 -0400
                  Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 04:02 +1100
                    Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:31 -0400
                      Re: Classes Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-31 12:39 -0500
                        Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:57 -0400
                      Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 10:40 -0700
                  Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-01 11:17 +1300
                    Re: Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:06 +1100
                      Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-01 00:12 -0600
                      Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 17:32 +1100
                      Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-03 00:32 +1300
              Re: Classes Dave Angel <davea@davea.name> - 2014-10-31 13:20 -0400
              Re: Classes Marko Rauhamaa <marko@pacujo.net> - 2014-10-31 19:35 +0200
              Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 17:40 +0000
              Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-31 18:26 -0400
                Re: Classes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 12:09 +1100
        Re: Classes   Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-01 04:06 +1100
          Re: Classes   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 13:18 -0400
            Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 10:43 -0700
              Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:18 -0400
                Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:42 -0400
                Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-31 18:57 -0400
                  Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 19:22 -0400
                    Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 19:32 -0400
                      Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-01 12:00 -0400
                        Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-01 12:42 -0400
                          Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-02 09:50 +0000
                            Re: Classes Tim Delaney <timothy.c.delaney@gmail.com> - 2014-11-02 21:32 +1100
                              Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-02 12:27 +0000
                                Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-02 11:31 -0500
                                  Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-03 03:12 +0000
                                    Re: Classes Dan Sommers <dan@tombstonezero.net> - 2014-11-03 06:29 +0000
                                      Re: Classes Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 01:10 +0000
                                        Re: Classes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-04 18:30 +1300
                                    Re: Classes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-03 08:50 -0500
                                    Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-04 02:34 +1100
                                Re: Classes Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-03 07:47 -0700
                            Re: Classes Larry Hudson <orgnut@yahoo.com> - 2014-11-02 14:03 -0800
              Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 07:51 +1100
                Re: Classes sohcahtoa82@gmail.com - 2014-10-31 14:24 -0700
                  Re: Classes Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-10-31 14:40 -0700
                    Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 08:56 +1100
                  Re: Classes Chris Angelico <rosuav@gmail.com> - 2014-11-01 08:36 +1100
            Re: Classes Grant Edwards <invalid@invalid.invalid> - 2014-10-31 17:43 +0000
      Re: Classes Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-31 14:11 +0000
      Re: Classes   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 12:49 -0400
        Re: Classes   "ast" <nomail@invalid.com> - 2014-10-31 19:31 +0100
          Re: Classes   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:36 -0400
          Re: Classes Joel Goldstick <joel.goldstick@gmail.com> - 2014-10-31 14:41 -0400
            Re: Classes Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-31 14:56 -0400

Page 2 of 4 — ← Prev page 1 [2] 3 4  Next page →


#80036

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-31 13:31 -0400
Message-ID<b3h75a5hj760euibf3m425a9jstofpk4n8@4ax.com>
In reply to#80028
On Sat, 1 Nov 2014 04:02:33 +1100, Chris Angelico <rosuav@gmail.com>
wrote:

>On Sat, Nov 1, 2014 at 3:47 AM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> inbuilt tutorial?
>>
>> The course is free.  You can't beat the price.  It is only for a few
>> more weeks.
>>
>> Trying to learn from reading the Internet has no set direction.  I
>> need a little nudge.  More like a shove.
>
>https://docs.python.org/3/tutorial/
>
>Same price.
>
I run across this page frequently.  To me, this is examples.  While
examples can be quite useful, I don't call this a tutorial.  I have
found the answer to my question by searching this page several times,
but the biggest problem with this page is that you can't copy and
paste the examples into IDLE.

JMO

[toc] | [prev] | [next] | [standalone]


#80038

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-31 12:39 -0500
Message-ID<mailman.15364.1414777205.18130.python-list@python.org>
In reply to#80036
On Fri, Oct 31, 2014 at 12:31 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> I run across this page frequently.  To me, this is examples.  While
> examples can be quite useful, I don't call this a tutorial.  I have
> found the answer to my question by searching this page several times,
> but the biggest problem with this page is that you can't copy and
> paste the examples into IDLE.

Sure you can, just click the little '>>>' button in the upper right of
the example box.

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#80044

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-31 13:57 -0400
Message-ID<76j75atqj6usgusrruknsamugofbpj8bfo@4ax.com>
In reply to#80038
On Fri, 31 Oct 2014 12:39:34 -0500, Zachary Ware
<zachary.ware+pylist@gmail.com> wrote:

>On Fri, Oct 31, 2014 at 12:31 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> I run across this page frequently.  To me, this is examples.  While
>> examples can be quite useful, I don't call this a tutorial.  I have
>> found the answer to my question by searching this page several times,
>> but the biggest problem with this page is that you can't copy and
>> paste the examples into IDLE.
>
>Sure you can, just click the little '>>>' button in the upper right of
>the example box.

Ah......so you can.  
Thanks

[toc] | [prev] | [next] | [standalone]


#80039

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2014-10-31 10:40 -0700
Message-ID<20141031104024.0d87773c@rg.highlandtechnology.com>
In reply to#80036
On Fri, 31 Oct 2014 13:31:44 -0400
Seymore4Head <Seymore4Head@Hotmail.invalid> wrote:

> On Sat, 1 Nov 2014 04:02:33 +1100, Chris Angelico <rosuav@gmail.com>
> wrote:
> 
> >On Sat, Nov 1, 2014 at 3:47 AM, Seymore4Head
> ><Seymore4Head@hotmail.invalid> wrote:
> >> inbuilt tutorial?
> >>
> >> The course is free.  You can't beat the price.  It is only for a few
> >> more weeks.
> >>
> >> Trying to learn from reading the Internet has no set direction.  I
> >> need a little nudge.  More like a shove.
> >
> >https://docs.python.org/3/tutorial/
> >
> >Same price.
> >
> I run across this page frequently.  To me, this is examples.  While
> examples can be quite useful, I don't call this a tutorial.  I have
> found the answer to my question by searching this page several times,
> but the biggest problem with this page is that you can't copy and
> paste the examples into IDLE.
> 
> JMO

No, but you can retype them.  Copying and pasting teaches nothing.
Figuring out how to hunt down the syntax errors your retyping has
induced teaches much.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#80063

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-11-01 11:17 +1300
Message-ID<cbig3bFekf1U1@mid.individual.net>
In reply to#80025
Seymore4Head wrote:
> The course is free.  You can't beat the price.  It is only for a few
> more weeks.

But if it's teaching you things that are blatantly wrong
in relation to Python, it may be doing more harm than
good.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#80069

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-01 12:06 +1100
Message-ID<54543231$0$13000$c3e8da3$5496439d@news.astraweb.com>
In reply to#80063
Gregory Ewing wrote:

> Seymore4Head wrote:
>> The course is free.  You can't beat the price.  It is only for a few
>> more weeks.
> 
> But if it's teaching you things that are blatantly wrong
> in relation to Python, it may be doing more harm than
> good.

Like all good Pythonistas[1], we hate Java and think that getter/setter
methods are pointless. But come on, they're not *wrong*, as in incorrect.
They do the job they're supposed to do, at the expense of an awkward
interface. Using properties is *exactly the same as Java getters/setters*,
except that the interface is less awkward.

And there are times when using getters and setters is the right choice.
Properties should only be used for quite lightweight calculations, because
attribute access is supposed to be fast. If your calculation is complex,
time-consuming or might fail, using a property is a bad idea and you should
use an explicit getter method, possibly with a setter if needed.

There are lots of things in Python which are written as getter methods or
functions, rather than attributes or properties. E.g.:

py> (23).bit_length()
5
py> len("hello world!")  # calls __len__ method
12

Files have quite a few examples. fileno, isatty and writable could all be
written as read-only properties, and seek/tell could be written as a
file.position property. But they're not, they're written as methods.





[1] Except for Jythonistas, who are allowed to enjoy using Java.

-- 
Steven

[toc] | [prev] | [next] | [standalone]


#80073

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-11-01 00:12 -0600
Message-ID<mailman.15377.1414822424.18130.python-list@python.org>
In reply to#80069
On Fri, Oct 31, 2014 at 7:06 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> And there are times when using getters and setters is the right choice.
> Properties should only be used for quite lightweight calculations, because
> attribute access is supposed to be fast. If your calculation is complex,
> time-consuming or might fail, using a property is a bad idea and you should
> use an explicit getter method, possibly with a setter if needed.

I agree except that in this scenario you should probably use a verb
other than "get", since getters should also be fast.

[toc] | [prev] | [next] | [standalone]


#80074

FromChris Angelico <rosuav@gmail.com>
Date2014-11-01 17:32 +1100
Message-ID<mailman.15378.1414823532.18130.python-list@python.org>
In reply to#80069
On Sat, Nov 1, 2014 at 5:12 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Oct 31, 2014 at 7:06 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> And there are times when using getters and setters is the right choice.
>> Properties should only be used for quite lightweight calculations, because
>> attribute access is supposed to be fast. If your calculation is complex,
>> time-consuming or might fail, using a property is a bad idea and you should
>> use an explicit getter method, possibly with a setter if needed.
>
> I agree except that in this scenario you should probably use a verb
> other than "get", since getters should also be fast.

Maybe. "get_track_length()" might be a reasonable method name for
something that opens an audio file, reads its header, and returns its
length in seconds. It should be a method rather than a property
(attribute access should be fast), but "get" makes sense here, as it's
not calculating or anything, it's just retrieving information. But
yes, if it's a calculation, "calc" would be more common.

ChrisA

[toc] | [prev] | [next] | [standalone]


#80116

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-11-03 00:32 +1300
Message-ID<cbmj2qFfr1vU1@mid.individual.net>
In reply to#80069
Steven D'Aprano wrote:
> Like all good Pythonistas[1], we hate Java and think that getter/setter
> methods are pointless. But come on, they're not *wrong*,

What's wrong is the statement that getters and setters
are necessary to allow the implementation to change
without changing the interface. That's factually
incorrect in regard to Python.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#80032

FromDave Angel <davea@davea.name>
Date2014-10-31 13:20 -0400
Message-ID<mailman.15362.1414776040.18130.python-list@python.org>
In reply to#80022
On 10/31/2014 12:31 PM, Seymore4Head wrote:

> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters. While not
> required by Python, this practice encourages the user of the class to
> manipulates class objects solely via class methods. The advantage of
> following this practice is that the implementer of the class
> definition (often someone other than the user of the class) may
> restructure the organization of the data fields associated with the
> object while avoiding the need to rewrite code that uses the class.
>

Written by somebody who doesn't understand a fundamental point about 
Python. It's just not true that the user would have to rewrite code if 
the implementer change the members.

The Python way of writing getters and setters (if/when you get to that 
stage of a Python class) is through the property decorator or its 
equivalent.

class Pet():
     def __init__(self, ....):
         self.internal details set here

     @property()
     def age(self):
         return some_complicated_expression

And the user of the class fetches this by using

mypet = Pet(....)
hisage = mypet.age

No stupid parentheses needed.  When the class is first written, no work 
is needed.  When it's updated to multiply the internal age by 7, just 
write the function, and decorate it to look like a regular attribute.

More complicated things can be done.  But the point is that the user 
should just get the age, by accessing the age attribute, and if the 
implementation needs to change, it can change.

The time to make a class complicated, is when it needs to be.  And the 
user should not have to pay the price for "just in case."

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#80037

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-10-31 19:35 +0200
Message-ID<87sii4rvl3.fsf@elektro.pacujo.net>
In reply to#80022
Seymore4Head <Seymore4Head@Hotmail.invalid>:

> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters.

The biggest problem in this OO disease is that it makes you think of
objects as data containers instead of dynamic agents.

An object is defined through its behavior, not what the black box
contains.


Marko

[toc] | [prev] | [next] | [standalone]


#80040

FromGrant Edwards <invalid@invalid.invalid>
Date2014-10-31 17:40 +0000
Message-ID<m30his$bbh$1@reader1.panix.com>
In reply to#80022
On 2014-10-31, Seymore4Head <Seymore4Head@Hotmail.invalid> wrote:
> On Fri, 31 Oct 2014 15:49:43 +0000 (UTC), Grant Edwards
><invalid@invalid.invalid> wrote:
>
>>On 2014-10-31, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>> On Fri, Oct 31, 2014 at 8:05 AM, Seymore4Head
>>><Seymore4Head@hotmail.invalid> wrote:
>>>> Because the topic of that lesson was getter setter.
>>>> I can construct an __init___  but I was practicing get/set.
>>>
>>> Doesn't sound like a very good lesson to me.
>>
>>It's not.  It's teaching java or C++ or some other language while
>>using a Python compiler.
>>
>>> Getters and setters are the Java way of doing things. The Pythonic
>>> way is to just use an attribute, and then replace it with a property
>>> in the unlikely event that getter/setter logic needs to be added.
>>
>>Exactly.
>
> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters.

IOW...

"In this class, we're not going to learn Python.  We're going to learn
Java. But actually _using_ Java is too much hassle, so we'll write
Java using Python instead."

> While not required by Python, this practice encourages the user of
> the class to manipulates class objects solely via class methods.

Which is a Java/C++ thing.

> The advantage of following this practice is that the implementer of
> the class definition (often someone other than the user of the class)
> may restructure the organization of the data fields associated with
> the object while avoiding the need to rewrite code that uses the
> class.

That's wrong.  That statement about using getter/setter having that
advantage is false (if you're talking about Python).  It may be true
in Java or C++ or whatever language the lesson's author is teaching,
but it's not true of Python.

-- 
Grant Edwards               grant.b.edwards        Yow! A shapely CATHOLIC
                                  at               SCHOOLGIRL is FIDGETING
                              gmail.com            inside my costume..

[toc] | [prev] | [next] | [standalone]


#80064

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-10-31 18:26 -0400
Message-ID<mailman.15374.1414794369.18130.python-list@python.org>
In reply to#80022
On Fri, 31 Oct 2014 12:31:56 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:

>
>In this class, we will follow the practice of accessing the contents
>of objects using methods known as getters and setters. While not
>required by Python, this practice encourages the user of the class to
>manipulates class objects solely via class methods. The advantage of
>following this practice is that the implementer of the class
>definition (often someone other than the user of the class) may
>restructure the organization of the data fields associated with the
>object while avoiding the need to rewrite code that uses the class.

	In Python, the conversion of direct attributes into properties does
exactly that -- NO REWRITE of using code...

	What you are being taught is NOT PYTHON.

-=-=-=-=-=-=-
def user():
    mySomething = Something()
    print mySomething.who
    print id(mySomething), id(Something)
    print mySomething.X
    mySomething.X = 123
    print mySomething.X
    print "********"
    

class Something(object):
    def __init__(self):
        self.X = None
        self.who = "I am the direct access version"

user()  #invoke a user with first class definition

#now redefine the class with properties
class Something(object):
    def __init__(self):
        self._X = None
        self.who = "I am the property version with getter/setter"
    def _setX(self, vlu):
        self._X = vlu
    def _getX(self):
        return self._X
    X = property(_getX, _setX)

user()  #invoke the SAME USER CODE but with class now using properties
-=-=-=-=-=-=-
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Wulfraed\Documents>cd "Python Progs"

C:\Users\Wulfraed\Documents\Python Progs>property.py
I am the direct access version
35663488 35817816
None
123
********
I am the property version with getter/setter
35663488 35818760
None
123
********

C:\Users\Wulfraed\Documents\Python Progs>

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#80070

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-01 12:09 +1100
Message-ID<545432bc$0$13000$c3e8da3$5496439d@news.astraweb.com>
In reply to#80064
Dennis Lee Bieber wrote:

> What you are being taught is NOT PYTHON.

Of course it is. It uses Python syntax, Python terminology, and the Python
compiler. It might not be the preferred Python idiom or best practice, but
it's still Python code.

Exaggeration does not help anyone, it just makes us look like religious
fanatics.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#80029

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-01 04:06 +1100
Message-ID<5453c1a6$0$12981$c3e8da3$5496439d@news.astraweb.com>
In reply to#80015
Seymore4Head wrote:

> Because the topic of that lesson was getter setter.
> I can construct an __init___  but I was practicing get/set.

What lesson is that? Using getters/setters is discouraged in Python.

> This stuff is coming to me slowly.  I need to rinse and repeat quite a
> few more times, before I follow what is going on.

Start with the simplest class possible:

class MyClass:
    pass

Right now, that class has a name, "MyClass", no methods, and no data. But we
can still create an instance. Call the class, as if it were a function, to
create the instance:

obj = MyClass()

What's the relationship between instances and classes? Classes are a general
type of entity, instances are a specific example of that entity. You can
have many instances from a class. So:

Class: Dog
Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
           Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.

Class: Wizard
Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.

Class: int
Instances: 0, 1, 2, -5, 23, 19874023, etc.


You can confirm that obj is now an instance of MyClass:

print(isinstance(obj, MyClass))

will print True.

What can you do with obj? It has no interesting methods, and no data. But we
can give it some! Python, unlike some languages, allows you to dynamically
add data attributes to instances on the fly, without pre-defining them.

obj.value = 23.0
obj.message = "hello world!"
print(obj.value)
print(obj.message)


will associate the data 23.0 and "hello world" to the attributes "value"
and "message" of the instance obj.

Let's make the class a bit easier to use, at the expense of doing a bit more
work up front:

class MyClass:
    def __init__(self, value, message):
        self.value = value
        self.message = message

obj = MyClass(23.0, "hello world")
print(obj.value)
print(obj.message)


The __init__ method is automatically called when you call the class as if it
were a function. Because the __init__ method has two arguments (plus the
special "self" argument), you have to call the class with two arguments.
They get used as the value and message respectively.


Or we can give it getters and setters:

class MyClass:
    def set_value(self, value):
        self.value = value
    def get_value(self):
        return self.value
    def set_message(self, message):
        self.message = message
    def get_message(self):
        return self.message

obj = MyClass()
obj.set_value(23.0)
obj.set_message("hello world")
print(obj.get_value())
print(obj.get_message())


If you're thinking that's a lot of extra work for not much benefit, 99.99%
of the time you're right.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#80033

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-31 13:18 -0400
Message-ID<7pg75alj1qn97hlirk81f7nqd265emgu05@4ax.com>
In reply to#80029
On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:

>Seymore4Head wrote:
>
>> Because the topic of that lesson was getter setter.
>> I can construct an __init___  but I was practicing get/set.
>
>What lesson is that? Using getters/setters is discouraged in Python.
>
>> This stuff is coming to me slowly.  I need to rinse and repeat quite a
>> few more times, before I follow what is going on.
>
>Start with the simplest class possible:
>
>class MyClass:
>    pass
>
>Right now, that class has a name, "MyClass", no methods, and no data. But we
>can still create an instance. Call the class, as if it were a function, to
>create the instance:
>
>obj = MyClass()
>
>What's the relationship between instances and classes? Classes are a general
>type of entity, instances are a specific example of that entity. You can
>have many instances from a class. So:
>
>Class: Dog
>Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
>           Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.
>
>Class: Wizard
>Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.
>
>Class: int
>Instances: 0, 1, 2, -5, 23, 19874023, etc.
>
>
>You can confirm that obj is now an instance of MyClass:
>
>print(isinstance(obj, MyClass))
>
>will print True.
>
>What can you do with obj? It has no interesting methods, and no data. But we
>can give it some! Python, unlike some languages, allows you to dynamically
>add data attributes to instances on the fly, without pre-defining them.
>
>obj.value = 23.0
>obj.message = "hello world!"
>print(obj.value)
>print(obj.message)
>
>
>will associate the data 23.0 and "hello world" to the attributes "value"
>and "message" of the instance obj.
>
>Let's make the class a bit easier to use, at the expense of doing a bit more
>work up front:
>
>class MyClass:
>    def __init__(self, value, message):
>        self.value = value
>        self.message = message
>
>obj = MyClass(23.0, "hello world")
>print(obj.value)
>print(obj.message)
>
>
>The __init__ method is automatically called when you call the class as if it
>were a function. Because the __init__ method has two arguments (plus the
>special "self" argument), you have to call the class with two arguments.
>They get used as the value and message respectively.
>
>
>Or we can give it getters and setters:
>
>class MyClass:
>    def set_value(self, value):
>        self.value = value
>    def get_value(self):
>        return self.value
>    def set_message(self, message):
>        self.message = message
>    def get_message(self):
>        return self.message
>
>obj = MyClass()
>obj.set_value(23.0)
>obj.set_message("hello world")
>print(obj.get_value())
>print(obj.get_message())
>
>
>If you're thinking that's a lot of extra work for not much benefit, 99.99%
>of the time you're right.

I agree it is more work.  But more work means more practice.  I need
more practice figuring out how these commands work.

>obj = MyClass()
>obj.set_value(23.0)
>obj.set_message("hello world")
>print(obj.get_value())
>print(obj.get_message())

I don't know here to get more (simple) practice problems.  I am trying
to invent my own.

[toc] | [prev] | [next] | [standalone]


#80041

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2014-10-31 10:43 -0700
Message-ID<20141031104319.02e1ca6f@rg.highlandtechnology.com>
In reply to#80033
On Fri, 31 Oct 2014 13:18:00 -0400
Seymore4Head <Seymore4Head@Hotmail.invalid> wrote:

> On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
> 
> >Seymore4Head wrote:
> >
> >> Because the topic of that lesson was getter setter.
> >> I can construct an __init___  but I was practicing get/set.
> >
> >What lesson is that? Using getters/setters is discouraged in Python.
> >
> >> This stuff is coming to me slowly.  I need to rinse and repeat quite a
> >> few more times, before I follow what is going on.
> >
> >Start with the simplest class possible:
> >
> >class MyClass:
> >    pass
> >
> >Right now, that class has a name, "MyClass", no methods, and no data. But we
> >can still create an instance. Call the class, as if it were a function, to
> >create the instance:
> >
> >obj = MyClass()
> >
> >What's the relationship between instances and classes? Classes are a general
> >type of entity, instances are a specific example of that entity. You can
> >have many instances from a class. So:
> >
> >Class: Dog
> >Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
> >           Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.
> >
> >Class: Wizard
> >Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.
> >
> >Class: int
> >Instances: 0, 1, 2, -5, 23, 19874023, etc.
> >
> >
> >You can confirm that obj is now an instance of MyClass:
> >
> >print(isinstance(obj, MyClass))
> >
> >will print True.
> >
> >What can you do with obj? It has no interesting methods, and no data. But we
> >can give it some! Python, unlike some languages, allows you to dynamically
> >add data attributes to instances on the fly, without pre-defining them.
> >
> >obj.value = 23.0
> >obj.message = "hello world!"
> >print(obj.value)
> >print(obj.message)
> >
> >
> >will associate the data 23.0 and "hello world" to the attributes "value"
> >and "message" of the instance obj.
> >
> >Let's make the class a bit easier to use, at the expense of doing a bit more
> >work up front:
> >
> >class MyClass:
> >    def __init__(self, value, message):
> >        self.value = value
> >        self.message = message
> >
> >obj = MyClass(23.0, "hello world")
> >print(obj.value)
> >print(obj.message)
> >
> >
> >The __init__ method is automatically called when you call the class as if it
> >were a function. Because the __init__ method has two arguments (plus the
> >special "self" argument), you have to call the class with two arguments.
> >They get used as the value and message respectively.
> >
> >
> >Or we can give it getters and setters:
> >
> >class MyClass:
> >    def set_value(self, value):
> >        self.value = value
> >    def get_value(self):
> >        return self.value
> >    def set_message(self, message):
> >        self.message = message
> >    def get_message(self):
> >        return self.message
> >
> >obj = MyClass()
> >obj.set_value(23.0)
> >obj.set_message("hello world")
> >print(obj.get_value())
> >print(obj.get_message())
> >
> >
> >If you're thinking that's a lot of extra work for not much benefit, 99.99%
> >of the time you're right.
> 
> I agree it is more work.  But more work means more practice.  I need
> more practice figuring out how these commands work.
> 
> >obj = MyClass()
> >obj.set_value(23.0)
> >obj.set_message("hello world")
> >print(obj.get_value())
> >print(obj.get_message())
> 
> I don't know here to get more (simple) practice problems.  I am trying
> to invent my own.

Fine.  Practice problem that is actually Pythonic.  Define a Rectangle
class.  Give it length and width attributes, no unnecessary
getters or setters.  Give it perimeter() and area() methods.

Define a Square class, subclassed from Rectangle.  Use getters/setters
to enforce that the length and width must be equal.  Confirm that
length and width remain locked, and that perimeter() and area() work
correctly.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#80048

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-31 14:18 -0400
Message-ID<cik75a5rjdg19jn7dacfqk6esdprbn71gi@4ax.com>
In reply to#80041
On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
<rgaddi@technologyhighland.invalid> wrote:


>Define a Square class, subclassed from Rectangle.  Use getters/setters
>to enforce that the length and width must be equal.  Confirm that
>length and width remain locked, and that perimeter() and area() work
>correctly.

class Rectangle:
    def __init__(self,length,width):
        self.length=length
        self.width=width
    def area(self):
        return self.length*self.width
    def perimeter(self):
        return 2*self.length+2*self.width
class Square(Rectangle):
    def set_side (self):
        if self.length!=self.width:
            
    
a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Rectangle(4,4)
print (c.area())
print (c.perimeter())

I bombed on the rest.

[toc] | [prev] | [next] | [standalone]


#80053

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-31 14:42 -0400
Message-ID<gvl75ah6l24i78rddlcpmnpomr3d5aku9a@4ax.com>
In reply to#80048
On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> wrote:

>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
><rgaddi@technologyhighland.invalid> wrote:
>
>
>>Define a Square class, subclassed from Rectangle.  Use getters/setters
>>to enforce that the length and width must be equal.  Confirm that
>>length and width remain locked, and that perimeter() and area() work
>>correctly.
>
>class Rectangle:
>    def __init__(self,length,width):
>        self.length=length
>        self.width=width
>    def area(self):
>        return self.length*self.width
>    def perimeter(self):
>        return 2*self.length+2*self.width
>class Square(Rectangle):
>    def set_side (self):
>        if self.length!=self.width:
>            
>    
>a=Rectangle(3,5)
>print (a.area())
>print (a.perimeter())
>b=Rectangle(5,7)
>print (b.area())
>print (b.perimeter())
>c=Rectangle(4,4)
>print (c.area())
>print (c.perimeter())
>
>I bombed on the rest.
class Rectangle:
    def __init__(self,length=0,width=0):
        self.length=length
        self.width=width
    def area(self):
        return self.length*self.width
    def perimeter(self):
        return 2*self.length+2*self.width
class Square(Rectangle):
    def set_side (self,side):
        self.side=side
        return side*side
            
    
a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Rectangle(4,4)
print (c.area())
print (c.perimeter())

A little closer.

[toc] | [prev] | [next] | [standalone]


#80065

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-10-31 18:57 -0400
Message-ID<mailman.15375.1414796262.18130.python-list@python.org>
In reply to#80048
On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:

>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
><rgaddi@technologyhighland.invalid> wrote:
>
>
>>Define a Square class, subclassed from Rectangle.  Use getters/setters
>>to enforce that the length and width must be equal.  Confirm that
>>length and width remain locked, and that perimeter() and area() work
>>correctly.
>
>class Rectangle:
>    def __init__(self,length,width):
>        self.length=length
>        self.width=width
>    def area(self):
>        return self.length*self.width
>    def perimeter(self):
>        return 2*self.length+2*self.width
>class Square(Rectangle):
>    def set_side (self):
>        if self.length!=self.width:
>           
	Where's the rest of that -- not to mention you called it "set_side" but
never pass a side into it for use.
 
-=-=-=-=-=-=-=-

class Rectangle(object):
    def __init__(self, length, width=None):
        self.length = length
        if width is None:
            self.width = length
        else:
            self.width = width
    def area(self):
        return self.length * self.width
    def perimeter(self):
        return 2 * (self.length + self.width)

class Square(Rectangle):
    def __init__(self, side):
        self.side = side
    def _getLength(self):
        return self.side
    def _getWidth(self):
        return self.side
    def _setLength(self, vlu):
        self.side = vlu
    def _setWidth(self, vlu):
        self.side = vlu
    length = property(_getLength, _setLength)
    width = property(_getWidth, _setWidth)

aRect = Rectangle(2, 4)
print aRect.length, aRect.width, aRect.area(), aRect.perimeter()
aRect.length = 9
print aRect.length, aRect.width, aRect.area(), aRect.perimeter()


aSqr = Square(3)
print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
aSqr.length = 4
print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
aSqr.width = 5
print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
aSqr.side = 7
print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()

-=-=-=-=-=-=-=-=-
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Wulfraed\Documents>cd "Python Progs"

C:\Users\Wulfraed\Documents\Python Progs>property2.py
2 4 8 12
9 4 36 26
3 3 3 9 12
4 4 4 16 16
5 5 5 25 20
7 7 7 49 28

C:\Users\Wulfraed\Documents\Python Progs>
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


Page 2 of 4 — ← Prev page 1 [2] 3 4  Next page →

Back to top | Article view | comp.lang.python


csiph-web