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

V

vasudevram

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.
 
A

alex23

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:
.... foo = 'FOO'
.... class Inner(object):
.... bar = '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.
 
V

vasudevram

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:




... foo = 'FOO'

... class Inner(object):

... bar = '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:
<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?
 
P

Peter Otten

Just one other doubt:


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'>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top