Object's nesting scope

Z

zaur

Hi folk!

What do you think about idea of "object's nesting scope" in python?

Let's imaging this feature, for example, in this syntax:

obj=<expression>:
<body>

or

<expression>:
<body>

That's means that result object of <expression> evaluation is used as
nested scope for <body> evaluation.

So is this idea useful?

Zaur
 
D

Diez B. Roggisch

zaur said:
Hi folk!

What do you think about idea of "object's nesting scope" in python?

Let's imaging this feature, for example, in this syntax:

obj=<expression>:
<body>

or

<expression>:
<body>

That's means that result object of <expression> evaluation is used as
nested scope for <body> evaluation.

So is this idea useful?

Whom am we to judge? Sure if you propose this, you have some usecases in
mind - how about you present these?

Diez
 
Z

zaur

Whom am we to judge? Sure if you propose this, you have some usecases in
mind - how about you present these

Ok. Here is a use case: object initialization.

For example,

person = Person():
name = "john"
age = 30
address = Address():
street = "Green Street"
no = 12

vs.

person = Person()
person.name = "john"
person.age = 30
address = person.address = Address()
address.street = "Green Street"
address.no = 12

In this example any assignment is an equivalence of setting
attribute's address of the parent object.
 
R

Rami Chowdhury

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?

If you are expecting those classes to be already defined, please bear in
mind that if you want, you can do this:
def __init__(self, name='Nemo', age=0, address=None):
self.name = name
self.age = age
self.address = address
def __init__(self, street=None, no=None):
self.street = street
self.no = no
name = 'Bob',
age = 26,
address = Address(
street = 'Blue Street',
no = 1
)
)
 
M

MRAB

zaur said:
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))
 
R

ryles

Hi folk!

What do you think about idea of "object's nesting scope" in python?

Let's imaging this feature, for example, in this syntax:

obj=<expression>:
     <body>

or

<expression>:
   <body>

That's means that result object of <expression> evaluation is used as
nested scope for <body> evaluation.

So is this idea useful?

Zaur

FYI, there was recently a similar idea being discussed on the python-
ideas list:

http://groups.google.com/group/python-ideas/browse_thread/thread/1a13cd9a5189b01c?hl=en
 
Z

zaur

zaur said:
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.
 
C

Carl Banks

Hi folk!

What do you think about idea of "object's nesting scope" in python?

Let's imaging this feature, for example, in this syntax:

obj=<expression>:
     <body>

or

<expression>:
   <body>

That's means that result object of <expression> evaluation is used as
nested scope for <body> evaluation.

So is this idea useful?

It might be marginally useful to save typing. The idea has been
discussed in various forms here quite a bit over the years. I doubt
there's any chance it'll be accepted into Python, because it goes
against one of the main design points of Python: that attributes
should always be accessed explicitly.

Having said that, the syntax you propose is awful. :) Normally when
this is proposed they use a keyword such as "using":

p = Person()
using p:
name = "Carl Banks"
location = "Los Angeles"

or, perhaps to save a line (even though it'd be a minor syntax abuse):

using Person() as p:
name = "Carl Banks"
location = "Los Angeles"


Carl Banks
 
Z

zaur

It might be marginally useful to save typing.
It also allow to structure the code.
The idea has been
discussed in various forms here quite a bit over the years.  I doubt
there's any chance it'll be accepted into Python, because it goes
against one of the main design points of Python: that attributes
should always be accessed explicitly.
I don't in general consider this idea as a way for implicit access to
object's attributes.
Idea is about to use in some way object's dictionary as nested scope
in a code block.

I agree though that using this only for saving typing or implicit
attribute access isn't a good idea.
Having said that, the syntax you propose is awful. :) Normally when
this is proposed they use a keyword such as "using":

p = Person()
using p:
    name = "Carl Banks"
    location = "Los Angeles"

or, perhaps to save a line (even though it'd be a minor syntax abuse):

using Person() as p:
    name = "Carl Banks"
    location = "Los Angeles"
I don't propose concrete syntax for using object's dictionary as
nested scope.
I used current only to explain the idea.
 
C

Carl Banks

I don't in general consider this idea as a way for implicit access to
object's attributes.

That's what is it regardless of what you consider it.
Idea is about to use in some way object's dictionary as nested scope
in a code block.

Which is implicitly accessing the object's attributes.


Carl Banks
 
Z

zaur

That's what is it regardless of what you consider it.


Which is implicitly accessing the object's attributes.

Carl Banks

In my opinion idea of using object's dictionary as nested scope is
more about structuring code blocks rather than just saving typing and
implicit attribute access.
 
B

Bruno Desthuilliers

zaur a écrit :
Ok. Here is a use case: object initialization.

For example,

person = Person():
name = "john"
age = 30
address = Address():
street = "Green Street"
no = 12

vs.

person = Person()
person.name = "john"
person.age = 30
address = person.address = Address()
address.street = "Green Street"
address.no = 12

Err... Looks like you really should read the FineManual(tm) -
specifically, the parts on the __init__ method.

class Person(object):
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address

class Address(object):
def __init__(self, street, no):
self.no = no
self.street = street


person = Person(
name="john",
age=30,
address = Address(
street="Green Street",
no=12
)
)
 
Z

zaur

zaur a écrit :







Err... Looks like you really should read the FineManual(tm) -
specifically, the parts on the __init__ method.

class Person(object):
    def __init__(self, name, age, address):
        self.name = name
        self.age = age
        self.address = address

class Address(object):
    def __init__(self, street, no):
        self.no = no
        self.street = street

person = Person(
    name="john",
    age=30,
    address = Address(
        street="Green Street",
        no=12
    )
)

What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?
 
M

Mel

zaur said:
What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?

My work-around would be:

p = person = Person()
p.name = "john"
p.age = age # assuming, for a moment, that the age I wanted to set
# happened to be in another variable
a = p.address = Address()
a.street = "Green Street"
a.no = 12

del a, p # optional, for a tidier namespace

Slightly verbose, but, saying what it means, it avoids having to guess which
age of several ages might have to be used in various places; the same would
go for other input variables. Actually, I probably wouldn't use p as a
stand-in for person unless person were a global name and p could be local.

Mel.
 
G

Gabriel Genellina

What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?

Welcome to dynamic languages! It doesn't matter *where* the class was
defined. You may add new attributes to the instance (even methods to the
class) at any time.

1)
person = Person()
vars(person).update(name="john",age=30,address=Address())
vars(person.Address).update(street="Green Street",no=12)

2)
def the_initializer_i_would_like(person, name, age):
person.name = name
person.age = age

person = Person()
the_initializer_i_would_like(person, name="john", age=30)

3)
def the_initializer_i_would_like(self, name, age):
self.name = name
self.age = age

Person.init = the_initializer_i_would_like
person = Person()
person.init(name="john", age=30)

4)
def a_generic_updater(obj, **kw):
try: ns = vars(obj)
except Exception: ns = None
if ns is not None:
ns.update(kw)
else:
for name in kw:
setattr(obj, name, kw[name])

person = Person()
a_generic_updater(person, name="john", age=30)
 
Z

zaur

En Fri, 28 Aug 2009 15:25:55 -0300, zaur <[email protected]> escribió:


What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?

Welcome to dynamic languages! It doesn't matter *where* the class was  
defined. You may add new attributes to the instance (even methods to the  
class) at any time.

1)
person = Person()
vars(person).update(name="john",age=30,address=Address())
vars(person.Address).update(street="Green Street",no=12)

2)
def the_initializer_i_would_like(person, name, age):
   person.name = name
   person.age = age

person = Person()
the_initializer_i_would_like(person, name="john", age=30)

3)
def the_initializer_i_would_like(self, name, age):
   self.name = name
   self.age = age

Person.init = the_initializer_i_would_like
person = Person()
person.init(name="john", age=30)

4)
def a_generic_updater(obj, **kw):
   try: ns = vars(obj)
   except Exception: ns = None
   if ns is not None:
     ns.update(kw)
   else:
     for name in kw:
       setattr(obj, name, kw[name])

person = Person()
a_generic_updater(person, name="john", age=30)

I know about these ways of object initializing. What I said is about
using object's dictionary as nested scope in code block. Object
initialization is just one use case.
So we say about different things.
 
G

Gabriel Genellina

On 28 авг, 16:07, Bruno Desthuilliers <bruno.
(e-mail address removed)> wrote:
zaur a écrit :
Ok. Here is a use case: object initialization.
Err... Looks like you really should read the FineManual(tm) -
specifically, the parts on the __init__ method.
What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?

Welcome to dynamic languages! It doesn't matter *where* the class was  
defined. You may add new attributes to the instance (even methods to
the class) at any time. [...4 examples...]

I know about these ways of object initializing. What I said is about
using object's dictionary as nested scope in code block. Object
initialization is just one use case.
So we say about different things.

Well, you asked how to proceed in certain cases and I showed several ways
it can be done right now, without requiring a new scope. You'll have to
think of another use case.

Attribute lookup is explicit in Python, and that's a very good thing. If
you follow the python-ideas thread posted earlier, you'll see the kind of
problems an implicit attribute lookup would cause. The "with" statement is
necesary (and a good thing) in Pascal, but not in Python.

Zope2 departs from this explicitness: it has a <dtml-with> construct
(similar to what you propose), and I hate it profoundly every time I have
to edit a DTML file - I can never tell *where* an attribute comes from.
Another related "feature" is acquisition, a stack of namespaces where
objects "inherit" attributes from their containers. Same thing, a complete
waste of time every time I have to track a bug.

Unless you can find a very compeling use case, I don't think this feature
will become part of the language anytime soon...
 
Z

zaur

En Sat, 29 Aug 2009 04:34:48 -0300, zaur <[email protected]> escribió:


En Fri, 28 Aug 2009 15:25:55 -0300, zaur <[email protected]> escribió:
On 28 авг, 16:07, Bruno Desthuilliers <bruno.
(e-mail address removed)> wrote:
zaur a écrit :
Ok. Here is a use case: object initialization.
Err... Looks like you really should read the FineManual(tm) -
specifically, the parts on the __init__ method.
What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?
Welcome to dynamic languages! It doesn't matter *where* the class was  
defined. You may add new attributes to the instance (even methods to  
the class) at any time. [...4 examples...]
I know about these ways of object initializing. What I said is about
using object's dictionary as nested scope in code block. Object
initialization is just one use case.
So we say about different things.

Well, you asked how to proceed in certain cases and I showed several ways  
it can be done right now, without requiring a new scope. You'll have to  
think of another use case.

Attribute lookup is explicit in Python, and that's a very good thing. If  
you follow the python-ideas thread posted earlier, you'll see the kind of  
problems an implicit attribute lookup would cause. The "with" statement is  
necesary (and a good thing) in Pascal, but not in Python.

Zope2 departs from this explicitness: it has a <dtml-with> construct  
(similar to what you propose), and I hate it profoundly every time I have  
to edit a DTML file - I can never tell *where* an attribute comes from.  
Another related "feature" is acquisition, a stack of namespaces where  
objects "inherit" attributes from their containers. Same thing, a complete  
waste of time every time I have to track a bug.

Unless you can find a very compeling use case, I don't think this feature  
will become part of the language anytime soon...


The same can be said about multiple inheritance.
However, multiple inheritance is a powerful tool in the hands of
someone who can properly and effectively use it.
 
B

Bruno Desthuilliers

zaur a écrit :
What are you doing if 1) classes Person and Address imported from
foreign module 2) __init__ method is not defined as you want?

Either 1/write an alternate initializer and bind it to the appropriate
classes or 2/write factory functions:

1/

from foreign_module import Person, Address

def my_init(self, **kw):
for k, v in kw.items():
setattr(self, k, v)
return self

Person.init = my_init
Address.init = my_init

person = Person().init(
name="john",
age=30,
address = Address().init(
street="Green Street",
no=12
)
)


2/

from functools import partial
import foreign_module

def my_factory(cls, **kw):
obj = cls()
for k, v in kw.items():
setattr(obj, k, v)
return obj

Person = partial(my_factory, foreign_module.Person)
Address = partial(my_factory, foreign_module.Address)

person = Person(
name="john",
age=30,
address = Address(
street="Green Street",
no=12
)
)
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top