انجمن کاربران لینوکس ایران - تکنوتاکس سابق
نحوه تعریف 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
    @@bar = 'bar!'        # class variable
    
    def self.bar=(var)    # class method, setter for "bar" variable
        @@bar  = var
    end
    
    def self.bar          # class method, getter for "bar" variable
        @@bar
    end
end
چه شکلی می شه همین کار رو توی پایتون انجام داد؟ مثلا من می دونم برای داشتن یه class method باید از classmethod@ استفاده کرد ولی نمی دونم چطور باید setter/getter براش نوشت یا چطوری یه class variable داشت. یکی دو روزه دارم گوگل می کنم ولی یه مقاله درست و حسابی پیدا نکردم. ممنون


Re: نحوه تعریف class method در پایتن - newbie - 2012-02-12

در پايتون خيلي سر راست تره :

کد :
class Book(object):

    def get_title(self):
        return self._title

    def set_title(self, title):
        self._title = title

    title = property(get_title, set_title)

Book.title = 'Code Complete'

البته در پايتون نيازي به اين كار ها هم نيست .... اين هم دليلش :

<!-- 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:
    titles = 'somethig'  # put a variable inside the class body, then automaticly it's `class varible'
    
    @classmethod
    def get_title(cls):
        return Book.titles
    
    @classmethod
    def set_title(cls, var):
        Book.titles = var
توی ترمینال:
کد :
Book().get_title()
Book().set_title('hello')
Book().get_title()
با این تفاوت که می خوام اسم تابع setter/getter یکی باشه یعنی می خوام قابلیت Syntactic sugar داشته باشه.


Re: نحوه تعریف class method در پایتن - newbie - 2012-03-07

خوب در واقع پایتون این حالت رو هم به راحتی ساپورت میکنه :

کد :
class C:
    @staticmethod
    def f(arg1, arg2, ...):

رفرنس خود پایتون کامل توضیح داده :
<!-- 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):
    @staticmethod
    def the_static_method(x):
        print x

MyClass.the_static_method(2) # outputs 2

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