Deutsch English Français Italiano |
<mailman.180.1719690794.2909.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: "Peter J. Holzer" <hjp-python@hjp.at> Newsgroups: comp.lang.python Subject: Re: Difference method vs attribut = function Date: Sat, 29 Jun 2024 21:53:05 +0200 Lines: 79 Message-ID: <mailman.180.1719690794.2909.python-list@python.org> References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de> <20240629195305.kxzys5ht7hottfup@hjp.at> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="d3b2qja7gqav7z6p" X-Trace: news.uni-berlin.de 5FQ9eP4huGpP78wFfHqHYgyQEXapYTLQxCl76ssRM5zQ== Cancel-Lock: sha1:ZzYlUfl9QpFanitbOIWwnmXnlHg= sha256:eAOJUmDSeQPnYFKXikhkAb6YjiIQl7ZoWHzW6bDA6I8= Return-Path: <hjp-python@hjp.at> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05; 'content-type:multipart/signed': 0.05; 'maintainers': 0.07; 'content-type:application/pgp-signature': 0.09; 'filename:fname piece:asc': 0.09; 'filename:fname piece:signature': 0.09; 'filename:fname:signature.asc': 0.09; 'instances': 0.09; 'methods,': 0.09; 'question:': 0.09; 'import': 0.15; '"creative': 0.16; '+0200,': 0.16; '__/': 0.16; 'attributes,': 0.16; 'challenge!"': 0.16; 'from:addr:hjp-python': 0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'reality.': 0.16; 'stross,': 0.16; 'subject: = ': 0.16; 'url-ip:212.17.106/24': 0.16; 'url- ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'code': 0.23; 'object': 0.26; 'function': 0.27; 'sense': 0.28; "doesn't": 0.32; 'context': 0.32; 'course.': 0.32; 'do.': 0.32; 'python- list': 0.32; 'but': 0.32; 'there': 0.33; 'hold': 0.33; 'header:In- Reply-To:1': 0.34; 'functions': 0.36; 'main': 0.37; 'class': 0.37; 'way': 0.38; 'could': 0.38; 'use': 0.39; 'both': 0.40; 'want': 0.40; 'should': 0.40; 'method': 0.61; 'here.': 0.61; 'received:212': 0.62; 'here': 0.62; 'skip:m 20': 0.63; 'pass': 0.64; 'well': 0.65; 'skip:t 20': 0.66; 'received:userid': 0.66; 'shows': 0.67; 'types': 0.67; 'per': 0.68; 'subject:method': 0.69; 'url-ip:212/8': 0.69; 'future': 0.72; 'yourself': 0.75; 'database': 0.80; 'attribute': 0.84; 'capture': 0.84; 'closure': 0.84; 'method,': 0.84; 'received:at': 0.84 Mail-Followup-To: python-list@python.org Content-Disposition: inline In-Reply-To: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language <python-list.python.org> List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> List-Archive: <https://mail.python.org/pipermail/python-list/> List-Post: <mailto:python-list@python.org> List-Help: <mailto:python-list-request@python.org?subject=help> List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> X-Mailman-Original-Message-ID: <20240629195305.kxzys5ht7hottfup@hjp.at> X-Mailman-Original-References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de> Bytes: 6410 --d3b2qja7gqav7z6p Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2024-06-28 18:08:54 +0200, Ulrich Goebel via Python-list wrote: > a class can have methods, and it can have attributes, which can hold a > function. Both is well known, of course. >=20 > My question: Is there any difference? >=20 > The code snipped shows that both do what they should do. But __dict__ > includes just the method, The other way around: It includes only the attributes, not the methods. > while dir detects the method and the > attribute holding a function. My be that is the only difference? >=20 >=20 > class MyClass: > def __init__(self): > functionAttribute =3D None > =20 > def method(self): > print("I'm a method") >=20 > def function(): > print("I'm a function passed to an attribute") Here is the other main difference: The object is not passed implicitely to the function. You have no way to access mc here. You can create a method on the fly with types.MethodType: import types mc.functionAttribute =3D types.MethodType(function, mc) > By the way: in my usecase I want to pass different functions to > different instances of MyClass. It is in the context of a database app > where I build Getters for database data and pass one Getter per > instance. Or in this case, since each function is specific to one instance, you could just use a closure to capture the object. But that might be confusing to any future maintainers (e.g. yourself in 6 months), if the method doesn't actually behave like a method. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --d3b2qja7gqav7z6p Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmaAZhgACgkQ8g5IURL+ KF1xXw//dkGR4zGztKyqgcQmdBjKjGHKI3V5GoPAzVIOsNYINwN+Fi5od678MYdX d3likfjZOOlJrJHI3NWL8uIU3yBbKfeB5yjXVH0UbYmzYixlkl8x9xhujtSGbTLm uQgV8FlVs4qQfo2ZZhmlRpSRQq4wdRbyQFDDwENm/BflEj64Efx+PbVi11xQgMg4 sTLWU84+xc2aVHs2vv6k8hzrZCUEM67m6uR9ftNZ/DNgCel6qEXs8sTDZ+t3/whj 6X6w3Ve1k0Pp41wdlU8Qy8YZ7jBtRcV703ardrMZx1O8dsWpsl7VPgxpZ+XO7zC0 IftF7b6xmaMiTsJ135of+AUwT0FpfTp23FrHe4rGGqBModru/MuU1cBA4JORAgQq LwRfrfWerUKRGKd04PoAEUFpmPcj35t7FHWv2SY0awHGs60aQjS/j0D+WKh0q78k NEZz0cEdfOq8pICdDwkyGP+0aNgat71wgW4NSMaqLQxiEICRxApP1AYUxl8QqQd6 ClVRdIDPrAFY8NYm2Tdp/TofH4dfTAGtJKZapUgPQJEvwMbac9lSYdafHqzFRDGN oR4FJgULf4bqQNLkmXeV9GtVIiMkDZ/ch+OnDbjMMU4k0o6iL5grYbuMe8kI2KEB OwoHl7QTgJFtsDsYr40o1dP40EZ27L4QKkE5KwD3ExuoVFwjMXQ= =AjUU -----END PGP SIGNATURE----- --d3b2qja7gqav7z6p--