![]() |
نحوه تعریف class method در پایتن - نسخه قابل چاپ +- انجمن کاربران لینوکس ایران - تکنوتاکس سابق (https://forum.sito.ir) +-- انجمن: پرسشها و پاسخها (https://forum.sito.ir/forumdisplay.php?fid=3) +--- انجمن: لینوکس و برنامه نویسی (https://forum.sito.ir/forumdisplay.php?fid=13) +--- موضوع: نحوه تعریف class method در پایتن (/showthread.php?tid=17715) |
نحوه تعریف class method در پایتن - serhossein - 2012-02-12 توی Ruby برای تعریف class method خیلی راحت از کلمه کلیدی self استفاده می کنیم و خیلی راحت هم می شه یه setter/getter برای یه class variable نوشت. برای مثال: کد : class Foo Re: نحوه تعریف class method در پایتن - newbie - 2012-02-12 در پايتون خيلي سر راست تره : کد : class Book(object): البته در پايتون نيازي به اين كار ها هم نيست .... اين هم دليلش : <!-- m --><a class="postlink" href="http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/">http://blog.fedecarg.com/2008/08/17/no- ... in-python/</a><!-- m --> Re: نحوه تعریف class method در پایتن - serhossein - 2012-02-13 من می خوام یه class method بنویسم نه یه object method این چیزی که شما می گی برای set/get کردن instance variable است .... اين هم دليلش : http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/ من چیزی می خوام شبیه این: کد : class Book: کد : Book().get_title() Re: نحوه تعریف class method در پایتن - newbie - 2012-03-07 خوب در واقع پایتون این حالت رو هم به راحتی ساپورت میکنه : کد : class C: رفرنس خود پایتون کامل توضیح داده : <!-- m --><a class="postlink" href="http://docs.python.org/library/functions.html#staticmethod">http://docs.python.org/library/function ... aticmethod</a><!-- m --> این هم یه نمونه مثال و رفرنس اون : کد : class MyClass(object): <!-- m --><a class="postlink" href="http://stackoverflow.com/questions/735975/static-methods-in-python">http://stackoverflow.com/questions/7359 ... -in-python</a><!-- m --> اما در مورد setter/getter در پایتون راستش خود من هیچ وقت این کار رو نکردم و نمیکنم. این مدل برنامه نویسی بیشتر در دنیای جاوا کاربرد داره تا پایتون (مسایل متدهای پابلیک و ... ). این رو ببینید : نقل قول :Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the ‘property’ built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in ‘property’. That means that until you prove that you need anything more than a simple attribute access, don’t write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.<!-- m --><a class="postlink" href="http://tomayko.com/writings/getters-setters-fuxors">http://tomayko.com/writings/getters-setters-fuxors</a><!-- m --> |