Object's nesting scope

MRAB python at mrabarnett.plus.com
Wed Aug 26 21:56:54 CEST 2009


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




More information about the Python-list mailing list