0
s = set([1,2,3])

I should be elegant to do the following:

a.update(s).update(s)

I doesn't work as I thought make a contains set([1,2,3,1,2,3,1,2,3])

So I'm wandering that Does Python advocate this chainable practise?

Junuxx
  • 14,011
  • 5
  • 41
  • 71
mko
  • 21,334
  • 49
  • 130
  • 191
  • 2
    For a method to "chain," it has to return the object that it's acting on. As for whether it's a good practice or not, in general you should only chain methods that perform a clear, well-defined mutation on an object, and not something that potentially has complex side effects. – jonvuri Oct 11 '12 at 13:01
  • 1
    `set([1,2,3,1,2,3,1,2,3])` will return `set([1, 2, 3])` filtering the duplicate items, as set has unique elements. – arulmr Oct 11 '12 at 13:01

5 Answers5

1

set.update() returns None so you can't chain updates like that

The usual rule in Python is that methods that mutate don't return the object

contrast with methods on immutable objects, which obviously must return a new object such as str.replace() which can be chained

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

It depends.

Methods that modify an object usually return None so you can't call a sequence of methods like this:

L.append(2).append(3).append(4)

And hope to have the same effect as:

L.append(2)
L.append(3)
L.append(4)

You'll probably get an AttributeError because the first call to append returns None and None does not have an append method.

Methods that creates new object returns that object, so for example:

"some string".replace("a", "b").replace("c", "d")

Is perfectly fine.

Usually immutable objects return new objects, while mutable ones return None but it depends on the method and the specific object.

Anyway it's certainly not a feature of the language itself but only a way to implement the methods. Chainable methods can be implemented in probably any language so the question "are python methods chainable" does not make much sense. The only reasonable question would be "Are python methods always/forced to be/etc. chainable?", and the answer to this question is "No".


In your example set can only contain unique items, so the result that you show does not make any sense. You probably wanted to use a simple list.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
0

And update method does not return you a set, rather a None value.
So, you cannot invoke another method update in chain on NoneType

So, this will anyways give you error..

a.update(s).update(s)

However, since a Set can contain only unique values. So, even if you separate your update on different lines, you won't get a Set like you want..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

Yes, you can chain method calls in Python. As to whether it's good practice, there are a number of libraries out there which advocate using chained calls. For example, SQLAlchemy's tutorial makes extensive use of this style of coding. You frequently encounter code snippets like

session.query(User).filter(User.name.in_(['Edwardo', 'fakeuser'])).all() 

A sensible guideline to adopt is to ask yourself whether it'll make the code easier to read and maintain. Always strive to make code readable.

CadentOrange
  • 3,263
  • 1
  • 34
  • 52
0

I write a simple example, chainable methods should always return an object ,like self,

class Chain(object):
    """Chain example"""
    def __init__(self):
        self._content = ''    
    def update(self, new_content):
        """method of appending content"""
        self._content += new_content
        return self
    def __str__(self):
        return self._content

>>> c = Chain()

>>> c.update('str1').update('str2').update('str3')

>>> print c
str1str2str3
iMom0
  • 12,493
  • 3
  • 49
  • 61