Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80033
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Classes |
| Date | 2014-10-31 13:18 -0400 |
| Message-ID | <7pg75alj1qn97hlirk81f7nqd265emgu05@4ax.com> (permalink) |
| References | <51755at03r0bidjqh3qf0hhpvjr8756ill@4ax.com> <545350c3$0$23449$426a74cc@news.free.fr> <bj575ahusmihp1fpm58gmcr2eg0g8k21o6@4ax.com> <5453c1a6$0$12981$c3e8da3$5496439d@news.astraweb.com> |
| Organization | Unlimited download news at news.astraweb.com |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web