Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44224 > unrolled thread
| Started by | vasudevram <vasudevram@gmail.com> |
|---|---|
| First post | 2013-04-23 14:50 -0700 |
| Last post | 2013-04-24 06:29 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
What is the reason for defining classes within classes in Python? vasudevram <vasudevram@gmail.com> - 2013-04-23 14:50 -0700
Re: What is the reason for defining classes within classes in Python? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 16:22 -0600
Re: What is the reason for defining classes within classes in Python? vasudevram <vasudevram@gmail.com> - 2013-04-23 16:13 -0700
Re: What is the reason for defining classes within classes in Python? alex23 <wuwei23@gmail.com> - 2013-04-23 17:50 -0700
Re: What is the reason for defining classes within classes in Python? vasudevram <vasudevram@gmail.com> - 2013-04-24 04:01 -0700
Re: What is the reason for defining classes within classes in Python? Peter Otten <__peter__@web.de> - 2013-04-24 14:00 +0200
Re: What is the reason for defining classes within classes in Python? vasudevram <vasudevram@gmail.com> - 2013-04-24 06:29 -0700
| From | vasudevram <vasudevram@gmail.com> |
|---|---|
| Date | 2013-04-23 14:50 -0700 |
| Subject | What is the reason for defining classes within classes in Python? |
| Message-ID | <4e46cc04-248e-4594-a24a-b272648a5e66@googlegroups.com> |
Hi list, I saw an example of defining a class within another class, here, in the docs for peewee, a simple ORM for Python: http://peewee.readthedocs.org/en/latest/peewee/quickstart.html In what way is this useful? Thanks, Vasudev
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-04-23 16:22 -0600 |
| Message-ID | <mailman.1001.1366755826.3114.python-list@python.org> |
| In reply to | #44224 |
On Tue, Apr 23, 2013 at 3:50 PM, vasudevram <vasudevram@gmail.com> wrote: > > Hi list, > > I saw an example of defining a class within another class, here, in the docs for peewee, a simple ORM for Python: > > http://peewee.readthedocs.org/en/latest/peewee/quickstart.html > > In what way is this useful? In that particular case they're just using it as a namespace. Django does the same thing.
[toc] | [prev] | [next] | [standalone]
| From | vasudevram <vasudevram@gmail.com> |
|---|---|
| Date | 2013-04-23 16:13 -0700 |
| Message-ID | <ebf43a37-c441-4a8a-9941-995377ae45e5@googlegroups.com> |
| In reply to | #44226 |
On Wednesday, April 24, 2013 3:52:57 AM UTC+5:30, Ian wrote: > On Tue, Apr 23, 2013 at 3:50 PM, vasudevram wrote: > > > > > > Hi list, > > > > > > I saw an example of defining a class within another class, here, in the docs for peewee, a simple ORM for Python: > > > > > > http://peewee.readthedocs.org/en/latest/peewee/quickstart.html > > > > > > In what way is this useful? > > > > In that particular case they're just using it as a namespace. Django > > does the same thing. Not clear. An example or more explanation might help, if you can. Thanks.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-04-23 17:50 -0700 |
| Message-ID | <2cec7512-997e-4f1f-9580-592495d449df@ys5g2000pbc.googlegroups.com> |
| In reply to | #44232 |
On Apr 24, 9:13 am, vasudevram <vasudev...@gmail.com> wrote:
> On Wednesday, April 24, 2013 3:52:57 AM UTC+5:30, Ian wrote:
> > On Tue, Apr 23, 2013 at 3:50 PM, vasudevram wrote:
> > > I saw an example of defining a class within another class
> > > In what way is this useful?
>
> > In that particular case they're just using it as a namespace.
>
> Not clear. An example or more explanation might help, if you can. Thanks.
Namespaces are used to allow for the same label to be applied to
different concepts without the labels conflicting with each other. If
I was writing a program that dealt with the mathematics of morality, I
might want to use the sine function and refer to it in the standard
way as 'sin', and I might also want to store a value representing your
lack of goodness as 'sin'. As you can't use the same label in the same
scope to refer to two different objects, one way of dealing with this
that lets you still use what you feel are the most appropriate names
is to put them into a namespace. So you could express this as:
class Math(object):
sin = function()
class Morality(object):
sin = True
Then in your code you can clearly distinguish between the two by using
Math.sin and Morality.sin. Modules & packages are also namespaces, so
in this example we'd replace the Math class with `import math`, which
has a sin function defined within it.
A nested class definition will be defined as an attribute of the class
its defined within:
>>> class Outer(object):
... foo = 'FOO'
... class Inner(object):
... bar = 'BAR'
...
>>> Outer.Inner
<class '__main__.Inner'>
>>> Outer.Inner.bar
'BAR'
With peewee, the Model class looks for a Meta attribute and uses
attributes on it to perform some tasks, like where to retrieve/store
the model data. This allows for a way of distinguishing between
attributes used to define fields, and attributes needed for those
tasks. It also means your Models can use field names that the class
would otherwise reserve for its own internal purposes:
class DatabaseDetails(Model):
# these attributes are fields
database = CharField()
owner = CharField()
# ...but the Meta attribute isn't
class Meta:
# these attributes are used by the Model class
database = db
Here, database as a field is a text string that could contain a
database name, while DatabaseDetails.Meta.database contains a
reference to an actual database where the DatabaseDetails record would
be stored.
[toc] | [prev] | [next] | [standalone]
| From | vasudevram <vasudevram@gmail.com> |
|---|---|
| Date | 2013-04-24 04:01 -0700 |
| Message-ID | <7143de25-61a1-4eaf-abd1-2eab03c913f8@googlegroups.com> |
| In reply to | #44239 |
On Wednesday, April 24, 2013 6:20:36 AM UTC+5:30, alex23 wrote: > On Apr 24, 9:13 am, vasudevram <vasudev...@gmail.com> wrote: > > > On Wednesday, April 24, 2013 3:52:57 AM UTC+5:30, Ian wrote: > > > > On Tue, Apr 23, 2013 at 3:50 PM, vasudevram wrote: > > > > > I saw an example of defining a class within another class > > > > > In what way is this useful? > > > > > > > In that particular case they're just using it as a namespace. > > > > > > Not clear. An example or more explanation might help, if you can. Thanks. > > > > Namespaces are used to allow for the same label to be applied to > > different concepts without the labels conflicting with each other. If > > I was writing a program that dealt with the mathematics of morality, I > > might want to use the sine function and refer to it in the standard > > way as 'sin', and I might also want to store a value representing your > > lack of goodness as 'sin'. As you can't use the same label in the same > > scope to refer to two different objects, one way of dealing with this > > that lets you still use what you feel are the most appropriate names > > is to put them into a namespace. So you could express this as: > > > > class Math(object): > > sin = function() > > > > class Morality(object): > > sin = True > > > > Then in your code you can clearly distinguish between the two by using > > Math.sin and Morality.sin. Modules & packages are also namespaces, so > > in this example we'd replace the Math class with `import math`, which > > has a sin function defined within it. > > > > A nested class definition will be defined as an attribute of the class > > its defined within: > > > > >>> class Outer(object): > > ... foo = 'FOO' > > ... class Inner(object): > > ... bar = 'BAR' > > ... > > >>> Outer.Inner > > <class '__main__.Inner'> > > >>> Outer.Inner.bar > > 'BAR' > > > > With peewee, the Model class looks for a Meta attribute and uses > > attributes on it to perform some tasks, like where to retrieve/store > > the model data. This allows for a way of distinguishing between > > attributes used to define fields, and attributes needed for those > > tasks. It also means your Models can use field names that the class > > would otherwise reserve for its own internal purposes: > > > > class DatabaseDetails(Model): > > # these attributes are fields > > database = CharField() > > owner = CharField() > > > > # ...but the Meta attribute isn't > > class Meta: > > # these attributes are used by the Model class > > database = db > > > > Here, database as a field is a text string that could contain a > > database name, while DatabaseDetails.Meta.database contains a > > reference to an actual database where the DatabaseDetails record would > > be stored. Actually, I did know what namespaces are in general. What I didn't get was how the inner class Meta in the peewee example was being used as a namespace. Your explanation makes things very clear. Thank you. Just one other doubt: > >>> Outer.Inner > > <class '__main__.Inner'> > In the above output, I would have thought Python would print __main__.Outer.Inner or Outer.Inner instead of __main__.Inner, since Inner is an attribute of Outer?
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-04-24 14:00 +0200 |
| Message-ID | <mailman.1019.1366804839.3114.python-list@python.org> |
| In reply to | #44254 |
vasudevram wrote:
> On Wednesday, April 24, 2013 6:20:36 AM UTC+5:30, alex23 wrote:
>>
>> A nested class definition will be defined as an attribute of the class
>>
>> its defined within:
>>
>>
>>
>> >>> class Outer(object):
>>
>> ... foo = 'FOO'
>>
>> ... class Inner(object):
>>
>> ... bar = 'BAR'
>>
>> ...
>>
>> >>> Outer.Inner
> Just one other doubt:
>
>> >>> Outer.Inner
>>
>> <class '__main__.Inner'>
>>
>
> In the above output, I would have thought Python would print
> __main__.Outer.Inner or Outer.Inner instead of __main__.Inner, since Inner
> is an attribute of Outer?
The Python developers seem to agree with you and have made the compiler
smart enough to accomodate your expectations in Python 3.3:
$ cat tmp.py
class Outer:
class Inner:
pass
print(Outer.Inner)
$ python3.2 tmp.py
<class '__main__.Inner'>
$ python3.3 tmp.py
<class '__main__.Outer.Inner'>
[toc] | [prev] | [next] | [standalone]
| From | vasudevram <vasudevram@gmail.com> |
|---|---|
| Date | 2013-04-24 06:29 -0700 |
| Message-ID | <8ec3f486-0996-4642-9d1a-90925a889307@googlegroups.com> |
| In reply to | #44263 |
Interesting. Thank you.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web