Object's nesting scope

zaur szport at gmail.com
Thu Aug 27 10:00:57 EDT 2009


On 26 авг, 23:56, MRAB <pyt... at mrabarnett.plus.com> wrote:
> zaur wrote:
> > 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
>
> [snip]
>
> Create factory functions:
>
> def new_address(**kwargs):
>      address = Address()
>      address.__dict__.update(kwargs)
>      return address
>
> def new_person(**kwargs):
>      person = Person()
>      person.__dict__.update(kwargs)
>      return person
>
> person = new_person(name="john", age=30,
> address=new_address(street="Green Street", no=12))

Original idea isn't about how to organize my code in order to
initialize these custom objects.
The idea is about to use object's dictionary as nested scope.



More information about the Python-list mailing list