Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > What is the reason for defining classes within classes in Python?

Reply
Thread Tools

What is the reason for defining classes within classes in Python?

 
 
vasudevram
Guest
Posts: n/a
 
      04-23-2013

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/lat...uickstart.html

In what way is this useful?

Thanks,
Vasudev
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      04-23-2013
On Tue, Apr 23, 2013 at 3:50 PM, vasudevram <(E-Mail Removed)> 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/lat...uickstart.html
>
> In what way is this useful?


In that particular case they're just using it as a namespace. Django
does the same thing.
 
Reply With Quote
 
 
 
 
vasudevram
Guest
Posts: n/a
 
      04-23-2013
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/lat...uickstart.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.
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      04-24-2013
On Apr 24, 9:13*am, vasudevram <(E-Mail Removed)> 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.
 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      04-24-2013
On Wednesday, April 24, 2013 6:20:36 AM UTC+5:30, alex23 wrote:
> On Apr 24, 9:13*am, vasudevram <(E-Mail Removed)> 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?
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-24-2013
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'>


 
Reply With Quote
 
vasudevram
Guest
Posts: n/a
 
      04-24-2013
Interesting. Thank you.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the advantage of using maven for java standalone app mcheung63@gmail.com Java 13 04-16-2013 01:42 AM
Windows 8 - so bad it's hastening the death of the PC? ~misfit~ NZ Computing 18 04-15-2013 04:15 AM
What Linux freeware will blur faces & show all frames of a 30second AVI video? Danny D. Digital Photography 8 04-14-2013 10:18 PM
How to move text within Jlabels to the right clusardi2k@aol.com Java 6 04-13-2013 07:20 PM



Advertisments