Object's nesting scope
zaur
szport at gmail.com
Wed Aug 26 14:57:54 EDT 2009
On 26 авг, 21:11, "Rami Chowdhury" <rami.chowdh... at gmail.com> wrote:
> > person = Person():
> > name = "john"
> > age = 30
> > address = Address():
> > street = "Green Street"
> > no = 12
>
> Can you clarify what you mean? Would that define a Person class, and an
> Address class?
I suppose that someone already define classes Person ans Address.
For example, in this stupid way in a foreign module:
class Person(object):
pass
class Address(object):
pass
and the following statements
person = Person():
name = "john"
age = 30
address = Address():
street = "Green Street"
no = 12
are constructing an instance as follows:
person = Person()
person.name = "john"
person.age = 30
address = person.address = Address()
address.street = "Green Street"
address.no = 12
> If you are expecting those classes to be already defined, please bear in
> mind that if you want, you can do this:
>
> > > > class Person(object):
>
> def __init__(self, name='Nemo', age=0, address=None):
> self.name = name
> self.age = age
> self.address = address
>
> > > > class Address(object):
>
> def __init__(self, street=None, no=None):
> self.street = street
> self.no = no
>
> > > > otherperson = Person(
>
> name = 'Bob',
> age = 26,
> address = Address(
> street = 'Blue Street',
> no = 1
> )
> )
>
Yes, that's right. I aware about this way of instance initialization.
More information about the Python-list
mailing list